首页 > 其他分享 >64. Minimum Path Sum

64. Minimum Path Sum

时间:2022-09-28 11:03:05浏览次数:46  
标签:right int Sum else Minimum grid path Path dp


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.

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
if(m == 0)
return 0;
int n = grid[0].size();
int dp[m][n];
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(i == 0 && j == 0)
dp[i][j] = grid[0][0];
else if(i == 0)
dp[i][j] = grid[i][j] + dp[i][j - 1];
else if(j == 0)
dp[i][j] = grid[i][j] + dp[i - 1][j];
else
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
}
}

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<" "<<dp[i][j];
cout<<endl;
}
return dp[m - 1][n - 1];


}
};

 

标签:right,int,Sum,else,Minimum,grid,path,Path,dp
From: https://blog.51cto.com/u_12504263/5718750

相关文章

  • python中sys.path.append('..')用法
    一般用处:import时,如果包不在同一个文件里,需要跨文件导入,则用sys.path.append('..')来更改导入的路径。例子:文件结构如图:主程序在code文件中,调用其他.py中的函数#mai......
  • 常用的函数式接口_Consumer接口的默认方法andThen和常用的函数式接口_Consumer接口练
    常用的函数式接口_Consumer接口的默认方法andThen: Consumer接口的默认方法andThen作用:需要两个Consumer接口,可以把两个Consumer接口组合到一起,在对数据进行消费例如:Co......
  • JadConfig classpathRepository 扩展
    JadConfig默认包含了基于内存,properties文件,系统属性,以及环境变量的Repository,但是对于classpath的文件处理不是很方便我们可以自己在扩展接口实现定义 ......
  • Xpath 高级用法
    xpath高级用法1.匹配当前节点下的所有:.//.表示当前//表示当前标签下的所有标签注:要配合使用2.匹配某标签的属性值:/@属性名称这里以input里的value值为例......
  • Many Many Paths(组合计数)
    题意给定\(r1,c1,r2,c2\),求\(\sum\limits_{i=r1}^{r2}\sum\limits_{j=c1}^{c2}f(i,j)\),其中\(f(i,j)\)表示从\((0,0)\)往上或者往右走到\((i,j)\)的方案数。题目链......
  • Arouter 无法找到 匹配路径(no match path)
    检查module是否采用了kotlin混合编程,如果是需要加上applyplugin:'kotlin-kapt'defaultConfig{minSdkVersionInteger.parseInt(MIN_SDK_VERSION)targetSdkVer......
  • [Oracle] LeetCode 76 Minimum Window Substring 双指针
    Giventwostringssandtoflengthsmandnrespectively,returntheminimumwindowsubstringofssuchthateverycharacterint(includingduplicates)isin......
  • 解决:xcrun: error: invalid active developer path (/Library/Developer/CommandLineT
    原因:因为我昨天更新了mac系统,可以看到xcode已经丢失了?解决方法那就重新装一遍xcode的命令行工具xcode-select--install......
  • ABC 270 C - Simple path(树+dfs)
    第一次写出比较正经的树+dfs,这不得写篇博客题目大意:给定一棵树,具有n个节点,给定n-1条边,给定一个起点和终点,让我们输出从起点到终点的路径。SampleInput1Copy5......
  • SUMS
    8.14T1简单贪心,照着题意模拟即可。T2给一棵树,有点权与边权,定义一棵子树\(f(x)\)为子树里权值为\(x\)的点两两距离和,并给定\(k_i\),求以\(i\)为根的子树里满足\(......