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问题,和上一题一样,JAVA实现如下:
static public int minPathSum(int[][] grid) { int[] v = new int[grid[0].length]; v[0]=grid[0][0]; for (int i = 1; i < v.length; i++) v[i] = grid[0][i]+v[i-1]; for (int i = 1; i < grid.length; i++) { v[0] += grid[i][0]; for (int j = 1; j < v.length; j++) v[j] = Math.min(v[j], v[j - 1]) + grid[i][j]; } return v[v.length - 1]; }