forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
41 lines (31 loc) · 997 Bytes
/
main.cpp
File metadata and controls
41 lines (31 loc) · 997 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
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/triangle/description/
/// Author : liuyubobobo
/// Time : 2018-03-26
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
for(int i = 1 ; i < n ; i ++){
triangle[i][0] += triangle[i-1][0];
triangle[i][i] += triangle[i-1][i-1];
for(int j = 1 ; j < i ; j ++)
triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j]);
}
return *min_element(triangle[n-1].begin(), triangle[n-1].end());
}
};
int main() {
vector<vector<int>> triangle = { {2},
{3, 4},
{6,5,7},
{4,1,8,3}};
cout << Solution().minimumTotal(triangle) << endl;
// 11
return 0;
}