-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathBestTimeToBuyAndSellStockV.cpp
More file actions
33 lines (28 loc) · 996 Bytes
/
BestTimeToBuyAndSellStockV.cpp
File metadata and controls
33 lines (28 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
class Solution {
public:
int solve(vector<int>& prices, int fees){
int n= prices.size();
vector<vector<int>> dp(n+1, vector<int>(2,0));
for(int index= n-1; index>=0; index--){
for(int buyOrSell= 0; buyOrSell<=1; buyOrSell++){
int profit= 0;
if(buyOrSell){
int buy= -prices[index] + dp[index+1][0];
int skip= dp[index+1][1];
profit= max(buy, skip);
}
else{
int sell= prices[index] + dp[index+1][1] - fees;
int skip= dp[index+1][0];
profit= max(sell, skip);
}
dp[index][buyOrSell]= profit;
}
}
return dp[0][1];
}
int maxProfit(vector<int>& prices, int fee) {
return solve(prices, fee);
}
};