【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
思路:由于只能向两个方向走,瞬间就没有了路线迂回的烦恼,题目的难度大大的降低了。先得到到达最上边和最左边的最小路径,其他的就从上面点和左边点中选一个路径短的路径加上。
当然,也可以空间上更简单一些,只缓存一行的数据,然后一行行的更新。但我懒得写了...
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <stack>
- using namespace std;
- class Solution {
- public:
- int minPathSum(vector<vector<int> > &grid) {
- if(grid.size() == )
- {
- return ;
- }
- vector<vector<int> > ans(grid.size(), vector<int>(grid[].size(), ));
- ans[][] = grid[][];
- for(int i = ; i < grid.size(); i++)
- {
- ans[i][] = ans[i - ][] + grid[i][];
- }
- for(int j = ; j < grid[].size(); j++)
- {
- ans[][j] = ans[][j - ] + grid[][j];
- }
- for(int i = ; i <grid.size(); i++)
- {
- for(int j = ; j <grid[].size(); j++)
- {
- ans[i][j] = min(ans[i - ][j], ans[i][j - ]) + grid[i][j];
- }
- }
- return ans.back().back();
- }
- };
【leetcode】Minimum Path Sum(easy)的更多相关文章
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 【LeetCode】113. Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【LeetCode】666. Path Sum IV 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【Leetcode】【Medium】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
随机推荐
- Maven初级学习(三)常用命令
依赖关系查看 mvn dependency:list #列表形式展示依赖 mvn dependency:tree #层级关系展示依赖 mvn dependency:analyze #依赖分析 声明周期 ...
- SQL 分组后获取其中一个字段最大值的整条记录
--有id,name,createDate的一张表testTable--根据name分组,获取每组中createDate最大的那条记录(整条)查询出来------------------------- ...
- github及其他记录
http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3 https://github.com/open-power-workgroup/Hospit ...
- 解析某些特殊格式XML文件时,获取不到根节点问题
还是在语音识别这块.在读取本地的SRGS的XML后,无法获取到根节点<grammar>. 下面是SRGS.XML文件(只给出了根节点) <?xml version="1.0 ...
- 不挣扎了,开始学习LINQ TO XML,进而来解析网页。
找到了别人遇到和我一样的问题:http://ylad.codeplex.com/discussions/430095(英文) 一位叫做Mister Goodcat的提供了信息: Short answe ...
- su root 和su - root 的区别
su - root is the same as su - just like login as root, then the shell is login shell,which mean i ...
- Java之properties文件读取
1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...
- Redis Windows下安装部署
下载Redis 在Redis的官网下载页上有各种各样的版本,我这次是在windows上部署的,要去GitHub上下载.我下载的是2.8.12版的,相信大家百度一下就可以搜到,这就是我们需要的: 启动R ...
- hdu5412——CRB and Queries
1.题目大意:区间第k大,单点修改 2.随便搞搞就好了= =,树套树或主席树,我写的很丑 #include <cstdio> #include <cstdlib> #inclu ...
- JavaScript获取onclick、onchange等事件值的代码
这里主要是用到了getAttributeNode()这个方法,它获取的是属性节点,忽略属性和事件的差别,具体示例如下,感兴趣的朋友可以参考下哈希望对大家有所帮助 今天小菜处理下拉菜单级联问题时,想获取 ...