-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120.Triangle
More file actions
28 lines (27 loc) · 791 Bytes
/
Copy path120.Triangle
File metadata and controls
28 lines (27 loc) · 791 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
27
28
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
vector<int> dp(triangle.size());
for(int i=0; i<triangle.size();i++) {
for(int j=i; j>=0;j--){
if(j==0)
dp[j]=triangle[i][j]+dp[j];
else
if(j==i)
dp[j]=triangle[i][j]+dp[j-1];
else{
int num=min(dp[j],dp[j-1]);
dp[j]=triangle[i][j]+num;
}
}
}
if(dp.size()==0)
return 0;
int res=dp[0];
for(int i=1;i<dp.size();i++){
if(dp[i]<res)
res=dp[i];
}
return res;
}
};