Advertisement

Minimum Falling Path Sum II - LeetCode 1289 Solution

Minimum Falling Path Sum II - Complete Solution Guide

Minimum Falling Path Sum II is LeetCode problem 1289, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an n x n integer matrix grid , return the minimum sum of a falling path with non-zero shifts . A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column. Example 1: Input: grid = [[1,2,3],[4,5,6],[7,8,9]] Output: 13 Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path w

Detailed Explanation

The problem asks us to find the minimum sum of a falling path through a given n x n integer matrix (grid). A falling path is defined as a selection of exactly one element from each row of the grid, with the constraint that no two elements chosen in adjacent rows can be in the same column. In simpler terms, you need to pick one number from each row such that the numbers you pick from adjacent rows are not in the same column, and you want the sum of your picked numbers to be as small as possible.

Solution Approach

The provided solution uses dynamic programming with space optimization. It iteratively calculates the minimum falling path sum for each cell in the grid, but instead of storing the entire DP table, it only stores the two smallest falling path sums encountered in the previous row, along with the column index of the smallest one. For each cell in the current row, it adds the cell's value to the minimum falling path sum from the previous row that is in a different column. It updates the two smallest path sums and their column index encountered in the current row and carries them over to the next row.

Step-by-Step Algorithm

  1. Step 1: Initialize `prev_min1` and `prev_min2` to 0 (representing the two smallest falling path sums from the previous row), and `prev_idx1` to -1 (representing the column index of the smallest falling path sum from the previous row).
  2. Step 2: Iterate through each row of the grid (from `i = 0` to `n-1`).
  3. Step 3: For each row `i`, initialize `curr_min1` and `curr_min2` to `float('inf')` (or `Integer.MAX_VALUE` in Java/C++ or `INT_MAX` in C) and `curr_idx1` to -1. These will store the two smallest falling path sums and column index for the current row.
  4. Step 4: Iterate through each column `j` in the current row `i`.
  5. Step 5: Calculate the cost of reaching the current cell `grid[i][j]` by adding its value to the appropriate minimum cost from the previous row. If the current column `j` is the same as `prev_idx1` (the column index of the smallest path sum in the previous row), add `prev_min2` (the second smallest path sum from the previous row). Otherwise, add `prev_min1` (the smallest path sum from the previous row).
  6. Step 6: Update `curr_min1`, `curr_min2`, and `curr_idx1` based on the calculated `cost`. If `cost` is smaller than `curr_min1`, update `curr_min2` to `curr_min1`, `curr_min1` to `cost`, and `curr_idx1` to `j`. Otherwise, if `cost` is smaller than `curr_min2`, update `curr_min2` to `cost`.
  7. Step 7: After iterating through all columns in the current row, update `prev_min1` to `curr_min1`, `prev_min2` to `curr_min2`, and `prev_idx1` to `curr_idx1`. These values will be used for the next row's calculations.
  8. Step 8: After iterating through all rows, return `prev_min1`, which represents the minimum falling path sum.

Key Insights

  • Insight 1: Dynamic programming is suitable for this problem because the minimum falling path sum for a given cell depends on the minimum falling path sums of the cells in the previous row that are in different columns.
  • Insight 2: Instead of storing all possible path sums, we only need to keep track of the two smallest path sums from the previous row and their corresponding column indices. This significantly reduces the space complexity.
  • Insight 3: The key to solving this problem efficiently is to avoid redundant calculations by storing intermediate results (the smallest and second smallest path sums from the previous row) and reusing them when calculating the path sums for the current row.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming, Matrix.

Companies

Asked at: Roblox.