-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.Triangle.cpp
More file actions
27 lines (25 loc) · 936 Bytes
/
28.Triangle.cpp
File metadata and controls
27 lines (25 loc) · 936 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
//Problem Description: https://leetcode.com/problems/triangle/
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
vector<vector<int>> mat(triangle.size());
mat[0].push_back(triangle[0][0]);
int ans = INT_MAX;
for (int i = 1; i < triangle.size(); i++)
{
for (int j = 0; j < triangle[i].size(); j++)
{
if (j == 0)
mat[i].push_back(triangle[i][j] + mat[i - 1][j]);
else if (j == triangle[i].size() - 1)
mat[i].push_back(triangle[i][j] + mat[i - 1][j - 1]);
else
mat[i].push_back(triangle[i][j] + min(mat[i - 1][j], mat[i - 1][j - 1]));
if (i == triangle.size() - 1)
ans = min(ans, mat[i][j]);
}
}
if (triangle.size() == 1) ans = triangle[0][0];
return ans;
}
};