This is one of those problems where the clean idea matters more than the amount of code. For 714. Best Time to Buy and Sell Stock with Transaction Fee, 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:
- TLE
- 20 / 44 test cases passed.
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, profits, hold.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//20 / 44 test cases passed.
03class Solution {
04public:
05 int maxProfit(vector<int>& prices, int fee) {
06 int N = prices.size();
07 vector<int> profits(N, 0);
08 int last = -1;
09
10 for(int i = 0; i < N; i++){
11 int notUseBefore = prices[i] - fee -
12 *min_element(prices.begin(), prices.begin()+i);
13 int noOp = (i > 0) ? profits[i-1] : 0;
14
15 // int useBefore = prices[i] - fee -
16 // *min_element(prices.begin()+last+1, prices.begin()+i) +
17 // ((last > 0) ? profits[last] : 0);
18 int useBefore = 0;
19 for(int last = 0; last < i; last++){
20 useBefore = max(useBefore,
21 prices[i] - fee -
22 *min_element(prices.begin()+last+1, prices.begin()+i) +
23 ((last > 0) ? profits[last] : 0)
24 );
25 }
26
27 if(max(notUseBefore, useBefore) > noOp){
28 last = i;
29 }
30 profits[i] = max({notUseBefore, useBefore, noOp});
31
32 // cout << profits[i] << endl;
33 }
34
35 return profits[N-1];
36 }
37};
38
39//DP
40//Runtime: 236 ms, faster than 10.33% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
41//Memory Usage: 60.1 MB, less than 5.88% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
42//time: O(N), space: O(N)
43class Solution {
44public:
45 int maxProfit(vector<int>& prices, int fee) {
46 int n = prices.size();
47 vector<int> hold(n+1, INT_MIN), empty(n+1, 0);
48
49 for(int i = 1; i <= n; i++){
50 //no op or buy
51 hold[i] = max(hold[i-1], empty[i-1]-prices[i-1]-fee);
52 //no op or sell
53 empty[i] = max(empty[i-1], hold[i-1]+prices[i-1]);
54 // cout << hold[i] << ", " << empty[i] << endl;
55 }
56
57 return max(hold[n], empty[n]);
58 }
59};
60
61//DP with O(1) space
62//Runtime: 232 ms, faster than 11.12% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
63//Memory Usage: 55.3 MB, less than 5.88% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
64//time: O(N), space: O(1)
65class Solution {
66public:
67 int maxProfit(vector<int>& prices, int fee) {
68 int n = prices.size();
69 int hold = INT_MIN, empty = 0;
70 int newhold;
71
72 for(int i = 1; i <= n; i++){
73 //no op or buy
74 newhold = max(hold, empty-prices[i-1]-fee);
75 //no op or sell
76 empty = max(empty, hold+prices[i-1]);
77 // cout << hold[i] << ", " << empty[i] << endl;
78 hold = newhold;
79 }
80
81 return max(hold, empty);
82 }
83};
84
85//Approach #1: Dynamic Programming with O(1) space
86//Runtime: 148 ms, faster than 32.31% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
87//Memory Usage: 13 MB, less than 100.00% of C++ online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
88//time: O(N), space: O(1)
89class Solution {
90public:
91 int maxProfit(vector<int>& prices, int fee) {
92 int cash = 0, hold = -prices[0];
93 for(int i = 1; i < prices.size(); i++){
94 /*
95 cash[i] = cash[i-1]: do nothing
96 cash[i] = hold+prices[i]-fee: sell the stock held in last day
97 */
98 cash = max(cash, hold + prices[i] - fee);
99 /*
100 hold[i] = hold[i-1]: do nothing
101 hold = cash[i-1] - prices[i]: buy stock today
102 */
103 hold = max(hold, cash - prices[i]);
104 }
105 return cash;
106 }
107};
Cost