LeetCode刷题笔录Minimum Path Sum
2014-07-04 05:56
357 查看
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.
标准的Dynamic Programming。注意第一行的元素只能从左边走过来,第一列的只能从上边走下来。
Note: You can only move either down or right at any point in time.
标准的Dynamic Programming。注意第一行的元素只能从左边走过来,第一列的只能从上边走下来。
public class Solution { public int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] result = new int[m] ; //fill the first row of result, i.e. result[0] int sum = 0; for(int i = 0; i < n; i++){ sum += grid[0][i]; result[0][i] = sum; } //fill the first column sum = 0; for(int j = 0; j < m; j++){ sum += grid[j][0]; result[j][0] = sum; } //fill the rest for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ result[i][j] = Math.min(result[i - 1][j], result[i][j - 1]) + grid[i][j]; } } return result[m - 1][n - 1]; } }
相关文章推荐
- leetcode--Minimum Path Sum
- [LeetCode]: 64: Minimum Path Sum
- leetcode 064 —— Minimum Path Sum
- [LeetCode] Minimum Path Sum
- 【leetcode】第64题 Minimum Path Sum 题目+解析+JAVA代码
- LeetCode 64: Minimum Path Sum
- [LeetCode] [动态规划] Minimum Path Sum
- LeetCode "Minimum Path Sum" - 2D DP
- 26_leetcode_Minimum path Sum
- leetcode - Minimum Path Sum
- leetcode64-Minimum Path Sum(最小路径和)
- [LeetCode] Minimum Path Sum 解题报告
- [Leetcode] Minimum Path Sum
- [LeetCode] Minimum Path Sum
- [Leetcode] Minimum Path Sum
- 68 leetcode - Minimum Path Sum
- LeetCode 064 Minimum Path Sum
- LeetCode算法题——Minimum Path Sum
- leetcode_c++:Minimum Path Sum (064)
- leetcode — minimum-path-sum