Java Elegant and easy-understanding solution


  • 0
    P
    public class Solution {
        public int minPathSum(int[][] grid) {
            int m = grid.length, n = grid[0].length;
            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);
            dp[0] = 0;
            for(int i=0; i<m; i++){
                dp[0] += grid[i][0];
                for(int j=1; j<n; j++) 
                    dp[j] = Math.min(dp[j-1], dp[j]) + grid[i][j];
            }
            return dp[n-1];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.