We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4e87ee1 commit cec2243Copy full SHA for cec2243
Minimum Falling Path Sum.java
@@ -0,0 +1,30 @@
1
+class Solution {
2
+ public void minSum(int[][] mat, int n, int r)
3
+ {
4
+ if(r < 0)
5
+ return;
6
+ for(int i = 0; i < n; i++)
7
8
+ int nextMin = mat[r + 1][i] + mat[r][i];
9
+ if(i > 0)
10
+ nextMin = Math.min(nextMin, mat[r + 1][i - 1] + mat[r][i]);
11
+ if(i < n - 1)
12
+ nextMin = Math.min(nextMin, mat[r + 1][i + 1] + mat[r][i]);
13
+ mat[r][i] = nextMin;
14
+ }
15
+ minSum(mat, n, r - 1);
16
17
+
18
+ public int minFallingPathSum(int[][] matrix)
19
20
+ int n = matrix.length;
21
+ minSum(matrix, n, n - 2);
22
+ int ans = Integer.MAX_VALUE;
23
24
25
+ if(ans>matrix[0][i])
26
+ ans=matrix[0][i];
27
28
+ return ans;
29
30
+}
0 commit comments