We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a7bfc35 commit ed42465Copy full SHA for ed42465
0120-triangle/0120-triangle.cpp
@@ -0,0 +1,22 @@
1
+class Solution {
2
+public:
3
+ int minimumTotal(vector<vector<int>>& triangle) {
4
+ int n = triangle.size();
5
+ const int inf = 1e9;
6
+ vector<vector<int>> dp(n + 5, vector<int>(n + 5, inf));
7
+ dp[0][0] = triangle[0][0];
8
+ for (int i = 0; i + 1 < n; i++) {
9
+ for (int j = 0; j <= i; j++) {
10
+ dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + triangle[i + 1][j]);
11
+ if (j + 1 < n) {
12
+ dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + triangle[i + 1][j + 1]);
13
+ }
14
15
16
+ int ans = inf;
17
+ for (int i = 0; i < n; i++) {
18
+ ans = min(ans, dp[n - 1][i]);
19
20
+ return ans;
21
22
+};
0 commit comments