Last active
November 18, 2017 06:15
-
-
Save songouyang/d22d908bcd789d9a613e8bb89b74f8e0 to your computer and use it in GitHub Desktop.
120. Triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Solution { | |
public: | |
int minimumTotal(vector<vector<int>>& triangle) { | |
if (triangle.empty()){ | |
return 0; | |
} | |
vector<int> dp (triangle.size()+1, INT_MAX); | |
dp[1] = triangle[0][0]; | |
for (int i = 1; i < triangle.size(); ++i) { | |
for (int j = i; j >= 0; --j) { | |
dp[j+1] = min(dp[j], dp[j+1]) + triangle[i][j]; | |
} | |
} | |
return *min_element(dp.begin(), dp.end()); | |
} | |
}; | |
int main(){ | |
vector<vector<int>> tri = { | |
{2}, | |
{3, 4}, | |
{6, 5, 7}, | |
{4, 1, 8, 3} | |
}; | |
Solution s; | |
cout << s.minimumTotal(tri) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment