Minimum Path Sum - Complete Solution Guide
Minimum Path Sum is LeetCode problem 64, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Example 1: Input: grid = [[1,3,1],[1,5,1],[4,2,1]] Output: 7 Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. Example 2: Input: grid = [[1,2,3],[4,5,6]] Output: 12 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 200 0 <= grid[i][
Detailed Explanation
The problem requires finding the minimum path sum from the top-left corner to the bottom-right corner of a given m x n grid filled with non-negative numbers. The allowed moves are only down or right at any point in time. The objective is to find the path that results in the smallest sum of numbers along the path.
Solution Approach
The solution uses dynamic programming to calculate the minimum path sum to each cell in the grid. The approach leverages the grid itself to store the intermediate results. First, the first row and first column are initialized based on the path from the top-left corner. Then, for each remaining cell, the minimum path sum is calculated by adding the current cell's value to the minimum of the path sums from the cell above and the cell to the left. Finally, the value in the bottom-right cell represents the overall minimum path sum.
Step-by-Step Algorithm
- Step 1: Initialize the first cell (top-left) as the starting point.
- Step 2: Iterate through the first row (excluding the first cell), and update each cell with the sum of itself and the cell to its left. This calculates the minimum path from the top-left to each cell in the first row.
- Step 3: Iterate through the first column (excluding the first cell), and update each cell with the sum of itself and the cell above it. This calculates the minimum path from the top-left to each cell in the first column.
- Step 4: Iterate through the rest of the grid (from index [1][1] to the bottom-right), and update each cell with the sum of itself and the minimum of the cell above and the cell to its left. grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]).
- Step 5: The value at grid[m-1][n-1] now contains the minimum path sum from the top-left to the bottom-right corner. Return this value.
Key Insights
- Insight 1: Dynamic programming can be used because the optimal path to a cell (i, j) depends only on the optimal paths to its immediate neighbors (i-1, j) and (i, j-1).
- Insight 2: The minimum path sum to cell (i, j) can be calculated as grid[i][j] + min(pathSum(i-1, j), pathSum(i, j-1)).
- Insight 3: Instead of creating a separate DP table, the input grid itself can be used to store the minimum path sums to each cell, reducing space complexity.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: Amazon, Apple, Bloomberg, Dream11, General Motors, Goldman Sachs, Google, Meta, Microsoft, Palo Alto Networks, Salesforce, TikTok, Uber, Yahoo, eBay.