Start learning free

LESSON 285 OF 410 · BACKTRACKING

Minimum Path Sum in Java (O(n * m) Time Complexity)

Practice O(2n) Read the full explanation

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:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

Note:

This plain recursive solution recomputes the same cells many times, so it runs in exponential time, O(2n + m), and uses O(n + m) extra space for the recursion stack.


STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · BACKTRACKING

    Minimum Path Sum in Java, explained

    Understanding the Problem

    The core challenge of this problem is to find the path from the top-left corner to the bottom-right corner of a grid that results in the minimum sum of the numbers along the path. The constraints are that you can only move right or down at any point in time.

    This problem is significant in various applications such as robotics (finding the shortest path in a grid), game development (pathfinding algorithms), and operations research (optimizing routes).

    Potential pitfalls include misunderstanding the movement constraints and not considering all possible paths.

    Approach

    To solve this problem, we can use dynamic programming. The idea is to build a 2D array dp where dp[i][j] represents the minimum path sum to reach cell (i, j).

    1. **Naive Solution**: A naive approach would involve exploring all possible paths using recursion, which would be highly inefficient due to the exponential number of paths.

    2. **Optimized Solution**: Using dynamic programming, we can build the solution iteratively. We initialize the top-left cell with the value of the grid's top-left cell. For each cell, we compute the minimum path sum by considering the minimum of the cell above and the cell to the left.

    Algorithm

    1. Initialize a 2D array dp with the same dimensions as the grid.

    2. Set dp[0][0] to grid[0][0].

    3. Fill the first row and first column of dp since they can only be reached from one direction.

    4. For each cell (i, j), compute dp[i][j] as the minimum of dp[i-1][j] and dp[i][j-1] plus grid[i][j].

    5. The value at dp[m-1][n-1] will be the minimum path sum.

    Code Implementation

    public class MinimumPathSum {
        public int minPathSum(int[][] grid) {
            int m = grid.length;
            int n = grid[0].length;
            
            // Create a 2D dp array
            int[][] dp = new int[m][n];
            
            // Initialize the top-left cell
            dp[0][0] = grid[0][0];
            
            // Fill the first row
            for (int i = 1; i < n; i++) {
                dp[0][i] = dp[0][i - 1] + grid[0][i];
            }
            
            // Fill the first column
            for (int i = 1; i < m; i++) {
                dp[i][0] = dp[i - 1][0] + grid[i][0];
            }
            
            // Fill the rest of the dp array
            for (int i = 1; i < m; i++) {
                for (int j = 1; j < n; j++) {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
                }
            }
            
            // The bottom-right cell contains the minimum path sum
            return dp[m - 1][n - 1];
        }
    }
    

    Complexity Analysis

    The time complexity of this approach is O(n * m) because we iterate through each cell of the grid once. The space complexity is also O(n * m) due to the additional 2D array dp.

    Edge Cases

    1. **Single Cell Grid**: The grid has only one cell. The output should be the value of that cell.

    2. **Single Row or Column**: The grid has only one row or one column. The path is straightforward, and the sum is the sum of all cells in that row or column.

    3. **Large Values**: Ensure the algorithm handles large values without overflow.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple 3x3 grid as given in the example.
    • Single cell grid.
    • Single row grid.
    • Single column grid.
    • Grid with large values.

    Thinking and Problem-Solving Tips

    1. Break down the problem into smaller subproblems and solve them iteratively.

    2. Use dynamic programming to store intermediate results and avoid redundant calculations.

    3. Practice similar problems to improve your problem-solving skills.

    Conclusion

    Understanding and solving the minimum path sum problem using dynamic programming is crucial for optimizing pathfinding algorithms. By breaking down the problem and using a systematic approach, we can efficiently find the solution.

    Practice and explore further to master dynamic programming techniques and apply them to various problems.

    Additional Resources

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free