-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy path120. Triangle.cpp
More file actions
25 lines (25 loc) · 797 Bytes
/
120. Triangle.cpp
File metadata and controls
25 lines (25 loc) · 797 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
class Solution {
public:
int minimumTotal(vector<vector<int>>& t) {
if(t.size() == 0)return 0;
if(t.size()==1)return t[0][0];
int ** dp = new int*[t.size()];
dp[0] = new int[t.size()*t.size()+1];
for(int i =1;i!=t.size();++i){
dp[i] = dp[i-1]+ t.size();
}
dp[0][0] = t[0][0];
for(int i =1;i!=t.size();++i){
dp[i][0] = dp[i-1][0]+t[i][0];
dp[i][i] = dp[i-1][i-1]+t[i][i];
for(int j =1;j!=i;++j){
dp[i][j] = t[i][j]+(dp[i-1][j-1]>dp[i-1][j]?dp[i-1][j]:dp[i-1][j-1]);
}
}
int min = dp[t.size()-1][0];
for(int i =1;i!=t.size();++i){
if(min>dp[t.size()-1][i])min = dp[t.size()-1][i];
}
return min;
}
};