Leetcode 64. Minimum Path Sum
2017-08-12 12:02
295 查看
题目
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.
思路
使用动态规划的思路dp[j] = Math.min(grid[i][j] + dp[j], grid[i][j] + d[j - 1])
代码
package leetcodeArray; public class Leetcode64MinimumPathSum { public int minPathSum(int[][] grid) { int hight = grid.length; int width = grid[0].length; int[] dp = new int[width]; dp[0] = grid[0][0]; for(int i = 1; i < width; ++i){ dp[i] = grid[0][i] + dp[i - 1]; } for (int i = 1; i < hight; ++i) { dp[0] = grid[i][0] + dp[0]; for (int j = 1; j < width; j++) { dp[j] = Math.min(grid[i][j] + dp[j], grid[i][j] + dp[j - 1]); } } return dp[width - 1]; } public static void main(String[] args){ Leetcode64MinimumPathSum test = new Leetcode64MinimumPathSum(); int[][] grid = {{1,4}, {1, 1}}; System.out.println(test.minPathSum(grid)); } }
相关文章推荐
- [LeetCode] 64. Minimum Path Sum
- [Leetcode] 64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- Leetcode:64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum(最小和的路径)
- LeetCode 64. Minimum Path Sum
- (Leetcode)64. Minimum Path Sum(medium)
- Leetcode:64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum(最小路径和)
- leetcode---64. Minimum Path Sum
- Leetcode-64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum(Python)
- leetcode_64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- [leetcode] 64. Minimum Path Sum