Woodstock Blog

a tech blog for general algorithmic interview questions

[LeetCode 64] Minimum Path Sum

Question

link

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.

Stats

Frequency 3
Difficulty 3
Adjusted Difficulty 2
Time to use --------

Ratings/Color = 1(white) 2(lime) 3(yellow) 4/5(red)

Analysis

This is a very easy DP problem.

But this is not a boring question because I found a few interesting solutions.

Solution

Code 1 is 2-D DP.

2nd code is from this blog. Instead of 2-D array, it simply uses a “rotational array” (滚动数组), or 1-D array.

3rd code is using no extra space. It works in place.

My code

2D array

public class Solution {
    public int minPathSum(int[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0) {
                    dp[i][j] = grid[i][j];
                } else if (i == 0) {
                    dp[i][j] = grid[i][j] + dp[i][j - 1];
                } else if (j == 0) {
                    dp[i][j] = grid[i][j] + dp[i - 1][j];
                } else {
                    dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

1-D array

public int minPathSum(int[][] grid) {
    if (grid.length == 0) return 0;
    if (grid[0].length == 0) return 0;
    int[] dp = new int[grid[0].length];
    for (int i = 0; i < grid.length; i ++) 
        for (int j = 0; j < grid[0].length; j ++) 
            if (i == 0 && j == 0) dp[j] = grid[0][0];
            else if (i == 0) dp[j] = dp[j-1] + grid[i][j];
            else if (j == 0) dp[j] += grid[i][j];
            else dp[j] = Math.min(dp[j-1], dp[j]) + grid[i][j];
    return dp[dp.length-1];
}

in-place

public int minPathSum(int[][] grid) {
    if (grid.length == 0) return 0;
    if (grid[0].length == 0) return 0;
    for (int i = 0; i < grid.length; i ++) 
        for (int j = 0; j < grid[0].length; j ++) 
            if (i == 0 && j == 0) continue;
            else if (i == 0) grid[i][j] = grid[i][j] + grid[i][j-1];
            else if (j == 0) grid[i][j] = grid[i][j] + grid[i-1][j];
            else grid[i][j] = Math.min(grid[i-1][j], grid[i][j-1]) + grid[i][j];
    return grid[grid.length-1][grid[0].length-1];
}