-
Notifications
You must be signed in to change notification settings - Fork 118
/
120. Triangle.cpp
66 lines (61 loc) · 2.15 KB
/
120. Triangle.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//DP
//Runtime: 8 ms, faster than 60.43% of C++ online submissions for Triangle.
//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Triangle.
//time: O(N^2), space: O(N^2)
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector<vector<int>> dp(n, vector(n, 0));
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
// cout << i << ", " << j << endl;
if(i == 0){
dp[i][j] = triangle[i][j];
}else{
//when j == 0, dp[i-1][j-1] is out of range
//when j == i, dp[i-1][j] is out of range
dp[i][j] = min(j > 0 ? dp[i-1][j-1] : INT_MAX,
j < i ? dp[i-1][j] : INT_MAX) +
triangle[i][j];
}
// cout << dp[i][j] << " ";
}
// cout << endl;
}
return *min_element(dp[n-1].begin(), dp[n-1].end());
}
};
//DP, O(n) space
//Runtime: 12 ms, faster than 26.32% of C++ online submissions for Triangle.
//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Triangle.
//time: O(N^2), space: O(N)
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector<int> dp(n, 0);
int ans = 0;
int dp_is1_js1, dp_is1_j;
for(int i = 0; i < n; i++){
/*
when j is 0, this is meaningless,
reset when meeting a new row
*/
dp_is1_js1 = INT_MAX;
for(int j = 0; j <= i; j++){
dp_is1_j = dp[j];
if(i == 0){
dp[j] = triangle[i][j];
}else{
dp[j] = min(j > 0 ? dp_is1_js1 : INT_MAX,
j < i ? dp[j] : INT_MAX) +
triangle[i][j];
}
dp_is1_js1 = dp_is1_j;
}
}
return *min_element(dp.begin(), dp.end());
}
};