LeetcodeMinimum Path Sum
Posted wuezs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetcodeMinimum Path Sum相关的知识,希望对你有一定的参考价值。
题目链接:https://leetcode.com/problems/minimum-path-sum/
题目:
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.
思路:
c[i][j]表示从左上到i,j这个位置最小路径和,则状态转移方程:c[i][j]=min{c[i-1][j],c[i][j-1]}
算法:
public int minPathSum(int[][] grid) {
int c[][] = new int[grid.length][grid[0].length];
c[0][0] = grid[0][0];
for (int i = 1; i <grid.length; i++) {
c[i][0] = c[i-1][0]+grid[i][0];
}
for(int j=1;j<grid[0].length;j++){
c[0][j] = c[0][j-1]+grid[0][j];
}
for (int i = 1; i <grid.length; i++) {
for (int j = 1; j <grid[0].length; j++) {
c[i][j] = Math.min(c[i-1][j], c[i][j-1])+grid[i][j];
}
}
return c[grid.length-1][grid[0].length-1];
}
以上是关于LeetcodeMinimum Path Sum的主要内容,如果未能解决你的问题,请参考以下文章
LeetcodeMinimum Window Substring
112. Path Sum 113. Path Sum II
python 图像归一化作业代码代编程代写图python作业
解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target(示例代