-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0047.cpp
More file actions
33 lines (33 loc) · 1.27 KB
/
DAY0047.cpp
File metadata and controls
33 lines (33 loc) · 1.27 KB
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
// 120. Triangle
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
// int i,j,height=triangle.size(),widthCurrent=1;
// for(i=1;i<height;i++){
// widthCurrent=triangle[i].size();
// for(j=0;j<widthCurrent;j++){
// if(j==0){
// triangle[i][j]+=triangle[i-1][0];
// }
// else if(j==triangle[i].size()-1){
// triangle[i][j]+=triangle[i-1][j-1];
// }
// else{
// triangle[i][j]+=min(triangle[i-1][j-1],triangle[i-1][j]);
// }
// }
// }
// int min_element=INT_MAX;
// for(int x=0;x<triangle[i-1].size();x++){
// if(triangle[i-1][x]<min_element) min_element=triangle[i-1][x];
// }
// return min_element;
int height=triangle.size()-1;
while(--height>=0){
for(int j=triangle[height].size()-1;j>=0;j--){
triangle[height][j]+=min(triangle[height+1][j+1],triangle[height+1][j]);
}
}
return triangle[0][0];
}
};