File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
leetcode/cpp/dynamic programming Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Best Time to Buy and Sell Stock with Transaction Fee
2
+ class Solution {
3
+ public:
4
+ int maxProfit (vector<int >& prices, int fee) {
5
+ int n=prices.size ();
6
+ int cash[n];
7
+ int hold[n];
8
+ hold[0 ]=-prices[0 ];
9
+ cash[0 ]=0 ;
10
+ for (int i=1 ;i<n;i++)
11
+ {
12
+ cash[i]=max (cash[i-1 ],hold[i-1 ]+prices[i]-fee);
13
+ // cash[i-1]///not selling just continuing with prev
14
+ // hold[i-1]+prices[i]-fee//selling at i so prev hold +prices[i]-tfee
15
+ hold[i]=max (max (hold[i-1 ],cash[i-1 ]-prices[i]),hold[i-1 ]-fee);
16
+ // hold[i-1]//not buying
17
+ // cash[i-1]-prices[i]//buying at i
18
+ // hold[i-1]-fee+profit[i]-profit[i]//selling at i and buying another one
19
+
20
+ }
21
+ return cash[n-1 ];
22
+ }
23
+ };
You can’t perform that action at this time.
0 commit comments