-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
52 lines (35 loc) · 1.01 KB
/
source.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// https://leetcode.com/problems/minimum-falling-path-sum/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minFallingPathSum(vector<vector<int>> &A) {
int side = A.size();
int dp[side][side];
memset(dp, 0, sizeof(dp));
// Set base cases (last row)
for (int i = 0; i < side; ++i)
dp[side - 1][i] = A[side - 1][i];
for (int y = side - 2; y >= 0; --y)
for (int x = 0; x < side; ++x)
{
dp[y][x] = INT_MAX;
if (x > 0)
dp[y][x] = min(dp[y][x], dp[y + 1][x - 1] + A[y][x]);
if (x < side - 1)
dp[y][x] = min(dp[y][x], dp[y + 1][x + 1] + A[y][x]);
dp[y][x] = min(dp[y][x], dp[y + 1][x] + A[y][x]);
}
int ans = INT_MAX;
for (int x = 0; x < side; ++x)
if (dp[0][x] < ans)
ans = dp[0][x];
return ans;
}
};
int main() {
Solution solution;
vector<vector<int>> input = {{1, 2, 3}, { 4, 5, 6 }, { 7, 8, 9 }};
assert(solution.minFallingPathSum(input) == 12);
return 0;
}