I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1547. Minimum Cost to Cut a Stick, the solution in this repository is mainly a DFS + memoization solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: DFS + memoization, dynamic programming.
The notes already sitting in the source point us in the right direction:
- dfs + memorization
- https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/780880/DP-with-picture-(Burst-Balloons)
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are dfs, minCost.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//dfs + memorization
02//https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/780880/DP-with-picture-(Burst-Balloons)
03//Runtime: 124 ms, faster than 25.00% of C++ online submissions for Minimum Cost to Cut a Stick.
04//Memory Usage: 10.4 MB, less than 25.00% of C++ online submissions for Minimum Cost to Cut a Stick.
05class Solution {
06public:
07 vector<vector<int>> memo;
08
09 int dfs(vector<int>& cuts, int i, int j){
10 if(memo[i][j] != INT_MAX){
11 return memo[i][j];
12 }
13
14 /*
15 there must be one cut inbetween cuts[i] and cuts[j],
16 so that we can cut this segment
17 */
18 if(j - i <= 1){
19 return memo[i][j] = 0;
20 }
21
22 /*
23 find a place(cuts[k]) to cut
24 cuts[j] - cuts[i] is the length of current segment
25 */
26 for(int k = i+1; k < j; ++k){
27 memo[i][j] = min(memo[i][j],
28 cuts[j] - cuts[i] + dfs(cuts, i, k) + dfs(cuts, k, j));
29 }
30
31 return memo[i][j];
32 };
33
34 int minCost(int n, vector<int>& cuts) {
35 /*
36 we can add these two elements into cuts,
37 because we will never actually cut in 0 and n
38 */
39 cuts.push_back(0);
40 cuts.push_back(n);
41
42 sort(cuts.begin(), cuts.end());
43
44 memo = vector<vector<int>>(cuts.size(), vector<int>(cuts.size(), INT_MAX));
45
46 return dfs(cuts, 0, cuts.size()-1);
47 }
48};
49
50//bottom-up DP
51//https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/780880/DP-with-picture-(Burst-Balloons)
52//Runtime: 80 ms, faster than 25.00% of C++ online submissions for Minimum Cost to Cut a Stick.
53//Memory Usage: 10.4 MB, less than 25.00% of C++ online submissions for Minimum Cost to Cut a Stick.
54class Solution {
55public:
56 int minCost(int n, vector<int>& cuts) {
57 /*
58 we can add these two elements into cuts,
59 because we will never actually cut in 0 and n
60 */
61 cuts.push_back(0);
62 cuts.push_back(n);
63
64 sort(cuts.begin(), cuts.end());
65
66 vector<vector<int>> dp(cuts.size(), vector<int>(cuts.size()));
67
68 for(int i = cuts.size()-1; i >= 0; --i){
69 //[cuts[i], cuts[j]] defines the segment
70 /*
71 note that j starts from i+2,
72 because [cuts[i],cuts[i+1]] should always be 0
73 */
74 for(int j = i+2; j < cuts.size(); ++j){
75 dp[i][j] = INT_MAX;
76 for(int k = i+1; k < j; ++k){
77 dp[i][j] = min(dp[i][j], cuts[j] - cuts[i] + dp[i][k] + dp[k][j]);
78 }
79 }
80 }
81
82 return dp[0][cuts.size()-1];
83 }
84};
Cost