-
Notifications
You must be signed in to change notification settings - Fork 359
/
s2.cpp
30 lines (30 loc) · 1.03 KB
/
s2.cpp
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
// OJ: https://leetcode.com/problems/minimum-falling-path-sum-ii/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
pair<int, int> getSmallestTwo(vector<int> &A) {
auto p = make_pair(-1, -1);
for (int i = 0; i < A.size(); ++i) {
if (p.first == -1 || A[i] < A[p.first]) {
p.second = p.first;
p.first = i;
} else if (p.second == -1 || A[i] < A[p.second]) p.second = i;
}
return p;
}
public:
int minFallingPathSum(vector<vector<int>>& A) {
int N = A.size();
if (N == 1) return A[0][0];
vector<vector<int>> dp(2, vector<int>(N));
for (int i = 0; i < N; ++i) dp[1][i] = A[0][i];
for (int i = 1; i < N; ++i) {
auto p = getSmallestTwo(dp[i % 2]);
for (int j = 0; j < N; ++j) {
dp[(i + 1) % 2][j] = A[i][j] + dp[i % 2][p.first == j ? p.second : p.first];
}
}
return *min_element(dp[N % 2].begin(), dp[N % 2].end());
}
};