Skip to content

Commit dcb3797

Browse files
authored
Merge pull request #182 from tejaswini22199/first
Leetcode solution added for 714(Best time to sell and buy with transaction fee) in cpp
2 parents c8a98ac + 38a189c commit dcb3797

File tree

1 file changed

+23
-0
lines changed
  • leetcode/cpp/dynamic programming

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
};

0 commit comments

Comments
 (0)