Given an n x n array of integers matrix, return the minimum sum of any falling path throughmatrix.
A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
We use dynamic programming to compute the minimum falling path sum for each cell by considering the three possible cells above it. We build up the solution row by row.
For each cell in the matrix, starting from the second row, update its value by adding the minimum of the three possible cells above (directly above, above-left, above-right).
The answer is the minimum value in the last row after processing all rows.
Recurrence Relation:
Let dp[i][j] be the minimum falling path sum to cell (i, j).
1
2
3
4
5
dp[i][j] = matrix[i][j] + min(
dp[i-1][j],
dp[i-1][j-1] if j > 0,
dp[i-1][j+1] if j < n-1
)
Base Case:
dp[0][j] = matrix[0][j] for all columns in the first row.
classSolution {
publicintminFallingPathSum(int[][] matrix) {
int n = matrix.length;
int[][] dp =newint[n][n];
for (int i = 0; i < n; i++)
dp[0][i]= matrix[0][i];
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
int minAbove = dp[i-1][j];
if (j > 0) minAbove = Math.min(minAbove, dp[i-1][j-1]);
if (j < n-1) minAbove = Math.min(minAbove, dp[i-1][j+1]);
dp[i][j]= matrix[i][j]+ minAbove;
}
}
int ans = dp[n-1][0];
for (int v : dp[n-1]) ans = Math.min(ans, v);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funminFallingPathSum(matrix: Array<IntArray>): Int {
val n = matrix.size
val dp = Array(n) { matrix[it].clone() }
for (i in1 until n) {
for (j in0 until n) {
var minAbove = dp[i-1][j]
if (j > 0) minAbove = minOf(minAbove, dp[i-1][j-1])
if (j < n-1) minAbove = minOf(minAbove, dp[i-1][j+1])
dp[i][j] += minAbove
}
}
return dp[n-1].minOrNull() ?:0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from typing import List
classSolution:
defminFallingPathSum(self, matrix: List[List[int]]) -> int:
n = len(matrix)
dp = [row[:] for row in matrix]
for i in range(1, n):
for j in range(n):
min_above = dp[i-1][j]
if j >0:
min_above = min(min_above, dp[i-1][j-1])
if j < n-1:
min_above = min(min_above, dp[i-1][j+1])
dp[i][j] += min_above
return min(dp[n-1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmin_falling_path_sum(matrix: Vec<Vec<i32>>) -> i32 {
let n = matrix.len();
letmut dp = matrix.clone();
for i in1..n {
for j in0..n {
letmut min_above = dp[i-1][j];
if j >0 {
min_above = min_above.min(dp[i-1][j-1]);
}
if j < n-1 {
min_above = min_above.min(dp[i-1][j+1]);
}
dp[i][j] += min_above;
}
}
*dp[n-1].iter().min().unwrap()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
minFallingPathSum(matrix: number[][]):number {
constn=matrix.length;
constdp=matrix.map(row=> [...row]);
for (leti=1; i<n; i++) {
for (letj=0; j<n; j++) {
letminAbove=dp[i-1][j];
if (j>0) minAbove= Math.min(minAbove, dp[i-1][j-1]);
if (j<n-1) minAbove= Math.min(minAbove, dp[i-1][j+1]);
dp[i][j] +=minAbove;
}
}
return Math.min(...dp[n-1]);
}
}