# leetcode 64. Minimum Path Sum-最小路径和|动态规划

【思路】

public class Solution {
public int minPathSum(int[][] grid) {
for(int i=1; i<grid.length; i++) grid[i][0] += grid[i-1][0];
for(int j=1; j<grid[0].length; j++) grid[0][j] += grid[0][j-1];
for (int i=1; i<grid.length; i++) {
for (int j=1; j<grid[0].length; j++) {
grid[i][j] = Math.min(grid[i][j-1], grid[i-1][j]) + grid[i][j];
}
}
return grid[grid.length-1][grid[0].length-1];
}
}
61 / 61 test cases passed. Runtime: 5 ms  Your runtime beats 26.34% of javasubmissions.

public class Solution {
public int minPathSum(int[][] grid) {
int[] dp = new int[grid.length];
dp[0] = grid[0][0];
for(int i=1; i<grid.length; i++) dp[i] = grid[i][0]+dp[i-1];
for(int j=1; j<grid[0].length; j++)
for(int i=0; i<grid.length; i++)
dp[i] = (i==0 ? dp[i] : Math.min(dp[i], dp[i-1])) + grid[i][j];
return dp[grid.length-1];
}
}
61 / 61 test cases passed. Runtime: 4 ms  Your runtime beats 51.38% of javasubmissions.

class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
dp = [0]*len(grid)
dp[0] = grid[0][0]
for i in range(1,len(grid)) :
dp[i] = dp[i-1] + grid[i][0]
for j in range(1, len(grid[0])) :
for i in range(len(grid)) :
dp[i] = min(dp[i], dp[i-1]) + grid[i][j] if i > 0 else dp[i]+grid[i][j]
return dp[len(grid)-1]
61 / 61 test cases passed. Runtime: 64 ms  Your runtime beats 86.01% of pythonsubmissions.

• 本文已收录于以下专栏：

## LeetCode 64. Minimum Path Sum 动态规划

64. Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top lef...

## 64. 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[63] 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-64]Minimum Path Sum 二维数组路径最小和

0. 题目概要Leetcode 64. Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a pa...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)