-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1289.cpp
More file actions
26 lines (24 loc) · 725 Bytes
/
1289.cpp
File metadata and controls
26 lines (24 loc) · 725 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
25
26
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& grid) {
int n = grid.size();
vector<int> dp(n, 0);
vector<int> dpTemp(n, 0);
for (int j = 0; j < n; ++j) dp[j] = grid[0][j];
for (int i = 1; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dpTemp[j] = INT_MAX;
for (int k = 0; k < n; ++k) {
if (j == k) continue;
dpTemp[j] = min(dpTemp[j], dp[k] + grid[i][j]);
}
}
dp = dpTemp;
}
int res = INT_MAX;
for (int j = 0; j < n; ++j) {
res = min(res, dp[j]);
}
return res;
}
};