-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1289.java
29 lines (27 loc) · 857 Bytes
/
1289.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int minFallingPathSum(int[][] A) {
int r = A.length, c = A[0].length;
for (int i = r - 1; i > 0; i--) {
int[] m1 = new int[]{20000, 0};
int[] m2 = new int[]{20000, 0};
for (int k = 0; k < c; k++) {
if (A[i][k] < m1[0]) {
m2 = m1.clone();
m1[0] = A[i][k];
m1[1] = k;
} else if (A[i][k] < m2[0]) {
m2[0] = A[i][k];
m2[1] = k;
}
}
for (int j = 0; j < c; j++) {
A[i-1][j] += (j != m1[1] ? m1[0] : m2[0]);
}
}
int res = 20000;
for (int i = 0; i < c; i++) {
res = Math.min(res, A[0][i]);
}
return res;
}
}