KatInterview

64. Minimum Path Sum

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the minimum path sum from the top-left corner to the bottom-right corner of a grid. We are only allowed to move down or right. This constraint structure, where the solution to a larger problem depends on solutions to smaller subproblems (reaching an intermediate cell), strongly suggests a dynamic programming approach.

Solution

Minimum Path Sum in Python

class Solution:
    def minPathSum(self, grid: list[list[int]]) -> int:
        m = len(grid)
        n = len(grid[0])

        # The path to any cell in the first row can only come from the left.
        for j in range(1, n):
            grid[0][j] += grid[0][j-1]

        # The path to any cell in the first column can only come from above.
        for i in range(1, m):
            grid[i][0] += grid[i-1][0]
        
        # For the rest of the grid, the minimum path to (i, j) is
        # the current value grid[i][j] plus the minimum of the paths
        # from the top (grid[i-1][j]) and from the left (grid[i][j-1]).
        for i in range(1, m):
            for j in range(1, n):
                grid[i][j] += min(grid[i-1][j], grid[i][j-1])
        
        # The bottom-right element will now hold the minimum path sum to reach it.
        return grid[m-1][n-1]

Minimum Path Sum in Java

class Solution {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        // The path to any cell in the first row can only come from the left.
        for (int j = 1; j < n; j++) {
            grid[0][j] += grid[0][j-1];
        }

        // The path to any cell in the first column can only come from above.
        for (int i = 1; i < m; i++) {
            grid[i][0] += grid[i-1][0];
        }

        // For the rest of the grid, the minimum path to (i, j) is
        // the current value grid[i][j] plus the minimum of the paths
        // from the top (grid[i-1][j]) and from the left (grid[i][j-1]).
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
            }
        }

        // The bottom-right element will now hold the minimum path sum to reach it.
        return grid[m-1][n-1];
    }
}

Minimum Path Sum in C++

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();

        // The path to any cell in the first row can only come from the left.
        for (int j = 1; j < n; ++j) {
            grid[0][j] += grid[0][j-1];
        }

        // The path to any cell in the first column can only come from above.
        for (int i = 1; i < m; ++i) {
            grid[i][0] += grid[i-1][0];
        }

        // For the rest of the grid, the minimum path to (i, j) is
        // the current value grid[i][j] plus the minimum of the paths
        // from the top (grid[i-1][j]) and from the left (grid[i][j-1]).
        for (int i = 1; i < m; ++i) {
            for (int j = 1; j < n; ++j) {
                grid[i][j] += min(grid[i-1][j], grid[i][j-1]);
            }
        }

        // The bottom-right element will now hold the minimum path sum to reach it.
        return grid[m-1][n-1];
    }
};

Minimum Path Sum in TypeScript

function minPathSum(grid: number[][]): number {
    const m = grid.length;
    const n = grid[0].length;

    // The path to any cell in the first row can only come from the left.
    for (let j = 1; j < n; j++) {
        grid[0][j] += grid[0][j-1];
    }

    // The path to any cell in the first column can only come from above.
    for (let i = 1; i < m; i++) {
        grid[i][0] += grid[i-1][0];
    }

    // For the rest of the grid, the minimum path to (i, j) is
    // the current value grid[i][j] plus the minimum of the paths
    // from the top (grid[i-1][j]) and from the left (grid[i][j-1]).
    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
        }
    }

    // The bottom-right element will now hold the minimum path sum to reach it.
    return grid[m-1][n-1];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.