-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1289.cpp
More file actions
24 lines (22 loc) · 762 Bytes
/
Copy path1289.cpp
File metadata and controls
24 lines (22 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int minFallingPathSum(const vector<vector<int>> &grid) const {
const int n = size(grid), m = size(grid[0]);
int first = 0, second = 0, pos = -1;
for (auto i = 0; i < n; i++) {
auto first2 = INT_MAX, second2 = INT_MAX, pos2 = -1;
for (auto j = 0; j < m; j++) {
const int crnt = grid[i][j] + (j != pos ? first : second);
if (crnt < first2) {
second2 = first2;
first2 = crnt;
pos2 = j;
} else {
second2 = min(second2, crnt);
}
}
first = first2, second = second2, pos = pos2;
}
return first;
}
};