-
Notifications
You must be signed in to change notification settings - Fork 0
/
1289. Minimum falling path sum II
38 lines (32 loc) · 1.1 KB
/
1289. Minimum falling path sum II
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
30
31
32
33
34
35
36
37
38
//1289. Minimum falling path sum II
class Solution {
public int minFallingPathSum(int[][] grid) {
int n = grid.length;
int first_min = Integer.MAX_VALUE, second_min = Integer.MAX_VALUE;
int first_index = -1, second_index = -1;
for(int i = 0; i < n; i++) {
if(i != 0) {
for(int j = 0; j < n; j++) {
if(j != first_index)
grid[i][j] += first_min;
else
grid[i][j] += second_min;
}
}
first_min = Integer.MAX_VALUE;
second_min = Integer.MAX_VALUE;
for(int j = 0; j < n; j++) {
if(grid[i][j] < first_min) {
second_min = first_min;
first_min = grid[i][j];
first_index = j;
}
else if (grid[i][j] < second_min) {
second_min = grid[i][j];
second_index = j;
}
}
}
return Math.min(first_min, second_min);
}
}