-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1289. Minimum Falling Path Sum II #266
Comments
The problem is based on finding the minimum falling path sum in a matrix where we can choose exactly one element from each row, but the element from two adjacent rows cannot come from the same column. This requires a dynamic programming approach, along with optimization strategies to reduce the time complexity. Key Points:
Approach:The problem can be solved using dynamic programming (DP) where we maintain two important variables:
Plan:
Let's implement this solution in PHP: 1289. Minimum Falling Path Sum II <?php
/**
* @param Integer[][] $grid
* @return Integer
*/
function minFallingPathSum(array $grid): int
{
$n = count($grid); // Get the size of the grid.
// Using descriptive names for variables to indicate their usage.
$minFirstPathSum = 0; // Stores the minimum sum of the first path.
$minSecondPathSum = 0; // Stores the minimum sum of the second best path.
$prevMinPathCol = -1; // Index of the column resulting in the minimum sum in the previous row.
// Infinity substitute for initialization (could use PHP_INT_MAX).
$kInfinity = PHP_INT_MAX;
// Iterate over each row in the input grid.
foreach ($grid as $row) {
$newMinFirstPathSum = $kInfinity; // The new minimum sum of the first path.
$newMinSecondPathSum = $kInfinity; // The new minimum sum of the second best path.
$newMinPathCol = -1; // Column index for the new minimum sum.
// Iterate over each element in the current row.
for ($j = 0; $j < $n; ++$j) {
$currentSum = ($prevMinPathCol != $j ? $minFirstPathSum : $minSecondPathSum) + $row[$j];
// If the current sum is less than the new minimum, update both the first and second best sums.
if ($currentSum < $newMinFirstPathSum) {
$newMinSecondPathSum = $newMinFirstPathSum; // Previous min becomes second best.
$newMinFirstPathSum = $currentSum; // Current sum becomes the new min.
$newMinPathCol = $j; // Update the column index for new min.
} else if ($currentSum < $newMinSecondPathSum) {
$newMinSecondPathSum = $currentSum; // Update the second best path sum if current is less than it.
}
}
// Update the path sums and the column index for the next row iteration.
$minFirstPathSum = $newMinFirstPathSum;
$minSecondPathSum = $newMinSecondPathSum;
$prevMinPathCol = $newMinPathCol;
}
return $minFirstPathSum; // Return the minimum sum of a falling path.
}
// Example usage:
$grid1 = [[1,2,3],[4,5,6],[7,8,9]];
$grid2 = grid = [[7]];
echo minFallingPathSum($grid1) . "\n"; // Output: 13
echo minFallingPathSum($grid2) . "\n"; // Output: 7
?> Explanation:
Example Walkthrough:Consider the matrix:
Output for Example:For the matrix:
The minimum falling path sum is Time Complexity:
This approach efficiently computes the minimum falling path sum using dynamic programming with optimization through the tracking of the two smallest values from the previous row. This solution scales well for larger matrices and avoids redundant recalculations, providing an optimal solution. |
Discussed in #265
Originally posted by mah-shamim August 9, 2024
Topics:
Array
,Dynamic Programming
,Matrix
Given an
n x n
integer matrixgrid
, 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:
grid = [[1,2,3],[4,5,6],[7,8,9]]
13
Example 2:
Constraints:
n == grid.length == grid[i].length
1 <= n <= 200
-99 <= grid[i][j] <= 99
Hint:
dp[i][j]
be the answer for the firsti
rows such that columnj
is chosen from rowi
.The text was updated successfully, but these errors were encountered: