The trick here is to name the state correctly, then let the implementation follow. For 120. Triangle, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
The notes already sitting in the source point us in the right direction:
- DP
- time: O(N^2), space: O(N^2)
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minimumTotal.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N^2), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//DP
02//Runtime: 8 ms, faster than 60.43% of C++ online submissions for Triangle.
03//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Triangle.
04//time: O(N^2), space: O(N^2)
05class Solution {
06public:
07 int minimumTotal(vector<vector<int>>& triangle) {
08 int n = triangle.size();
09 vector<vector<int>> dp(n, vector(n, 0));
10 int ans = 0;
11
12 for(int i = 0; i < n; i++){
13 for(int j = 0; j <= i; j++){
14 // cout << i << ", " << j << endl;
15 if(i == 0){
16 dp[i][j] = triangle[i][j];
17 }else{
18 //when j == 0, dp[i-1][j-1] is out of range
19 //when j == i, dp[i-1][j] is out of range
20 dp[i][j] = min(j > 0 ? dp[i-1][j-1] : INT_MAX,
21 j < i ? dp[i-1][j] : INT_MAX) +
22 triangle[i][j];
23 }
24 // cout << dp[i][j] << " ";
25 }
26 // cout << endl;
27 }
28
29 return *min_element(dp[n-1].begin(), dp[n-1].end());
30 }
31};
32
33//DP, O(n) space
34//Runtime: 12 ms, faster than 26.32% of C++ online submissions for Triangle.
35//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Triangle.
36//time: O(N^2), space: O(N)
37class Solution {
38public:
39 int minimumTotal(vector<vector<int>>& triangle) {
40 int n = triangle.size();
41 vector<int> dp(n, 0);
42 int ans = 0;
43 int dp_is1_js1, dp_is1_j;
44
45 for(int i = 0; i < n; i++){
46 /*
47 when j is 0, this is meaningless,
48 reset when meeting a new row
49 */
50 dp_is1_js1 = INT_MAX;
51 for(int j = 0; j <= i; j++){
52 dp_is1_j = dp[j];
53 if(i == 0){
54 dp[j] = triangle[i][j];
55 }else{
56 dp[j] = min(j > 0 ? dp_is1_js1 : INT_MAX,
57 j < i ? dp[j] : INT_MAX) +
58 triangle[i][j];
59 }
60 dp_is1_js1 = dp_is1_j;
61 }
62 }
63
64 return *min_element(dp.begin(), dp.end());
65 }
66};
Cost