A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 309. Best Time to Buy and Sell Stock with Cooldown, the solution in this repository is mainly a dynamic programming 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: dynamic programming.
The notes already sitting in the source point us in the right direction:
- inspired by 188. Best Time to Buy and Sell Stock IV.cpp
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 maxProfit, hold, s0, s1, s2.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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) 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//inspired by 188. Best Time to Buy and Sell Stock IV.cpp
02//Runtime: 608 ms, faster than 5.03% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
03//Memory Usage: 167.6 MB, less than 7.41% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
04class Solution {
05public:
06 int maxProfit(vector<int>& prices) {
07 int n = prices.size();
08
09 if(n == 0) return 0;
10
11 //pad 1 transactions, pad 2 days
12 vector<vector<int>> dp(n+1, vector(n+2, 0));
13
14 for(int i = 1; i <= n; i++){ //transactions
15 vector<int> hold(n+2, INT_MIN);
16 for(int j = 2; j < n+2; j++){ //days
17 dp[i][j] = max(dp[i][j-1], prices[j-2]+hold[j-1]);
18 hold[j] = max(hold[j-1], dp[i-1][j-2] - prices[j-2]);
19 }
20// cout << "dp" << endl;
21// for(int j = 2; j < n+2; j++){
22// cout << dp[i][j] << " ";
23// }
24// cout << endl;
25
26// cout << "hold" << endl;
27// for(int j = 2; j < n+2; j++){
28// cout << hold[j] << " ";
29// }
30// cout << endl;
31
32// cout << "--------" << endl;
33 }
34
35 return dp[n][n+1];
36 }
37};
38
39//Finite state machine
40//https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75928/Share-my-DP-solution-(By-State-Machine-Thinking)
41//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
42//Memory Usage: 11.5 MB, less than 7.41% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
43class Solution {
44public:
45 int maxProfit(vector<int>& prices) {
46 int n = prices.size();
47 if(n == 0) return 0;
48 vector<int> s0(n, 0); //after "sell+rest"(s2) or "s0", do "rest" or "buy"
49 vector<int> s1(n, 0); //after "buy"(s0) or "s1", do "rest" or "sell"
50 vector<int> s2(n, 0); //after "sell"(s1), do "rest"
51
52 s0[0] = 0;
53 s1[0] = -prices[0];
54 s2[0] = 0;
55
56 for(int i = 1; i < n; i++){
57 s0[i] = max(s0[i-1], s2[i-1]);
58 s1[i] = max(s1[i-1], s0[i-1]-prices[i]);
59 s2[i] = s1[i-1]+prices[i];
60 }
61
62 return max(s0[n-1], s2[n-1]);
63 }
64};
65
66//1-D DP
67//https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75927/Share-my-thinking-process
68//Runtime: 4 ms, faster than 60.34% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
69//Memory Usage: 11.5 MB, less than 7.41% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
70class Solution {
71public:
72 int maxProfit(vector<int>& prices) {
73 int n = prices.size();
74 if(n == 0) return 0;
75 //padding
76 vector<int> buy(n+1, 0);
77 vector<int> sell(n+1, 0);
78 prices.insert(prices.begin(), 0);
79
80 buy[1] = -prices[1];
81
82 for(int i = 2; i <= n; i++){
83 buy[i] = max(buy[i-1], sell[i-2]-prices[i]);
84 sell[i] = max(sell[i-1], buy[i-1]+prices[i]);
85 }
86
87 return sell[n];
88 }
89};
90
91//O(1) space DP
92//https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75927/Share-my-thinking-process
93//Runtime: 4 ms, faster than 60.34% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
94//Memory Usage: 11.4 MB, less than 7.41% of C++ online submissions for Best Time to Buy and Sell Stock with Cooldown.
95class Solution {
96public:
97 int maxProfit(vector<int>& prices) {
98 int n = prices.size();
99 if(n == 0) return 0;
100
101 int buy= -prices[0], prev_buy, sell = 0, prev_sell = 0;
102
103 for(int i = 1; i < n; i++){
104 prev_buy = buy;
105 //note that prev_sell here is 2 days ago
106 buy = max(buy, prev_sell-prices[i]);
107 //note that prev_sell here is 1 day ago
108 prev_sell = sell;
109 //note that prev_buy here is 1 day ago
110 sell = max(sell, prev_buy+prices[i]);
111 // cout << prev_buy << ", " << buy << ", " << prev_sell << ", " << sell << endl;
112 }
113
114 return sell;
115 }
116};
Cost