-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimeToBuyAndSellStockWithCountdown.cpp
More file actions
57 lines (51 loc) · 1.83 KB
/
Copy pathBestTimeToBuyAndSellStockWithCountdown.cpp
File metadata and controls
57 lines (51 loc) · 1.83 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1]
Output: 0
*/
/// Recursive DP
class Solution {
public:
int solve(int index, int buy, vector<int> &prices, vector<vector<int>> &dp){
if(index>=prices.size()){
return 0;
}
if(dp[index][buy]!=-1){
return dp[index][buy];
}
if(buy==1){
return dp[index][buy]=max(-prices[index]+solve(index+1,0,prices,dp), 0+solve(index+1,1,prices,dp));
}
return dp[index][buy]=max(prices[index]+solve(index+2,1,prices,dp), 0+solve(index+1,0,prices,dp));
}
int maxProfit(vector<int>& prices) {
int n=prices.size();
vector<vector<int>> dp(n,vector<int>(2,-1));
return solve(0,1,prices,dp);
}
};
/// Optimized
int stockProfit(vector<int> &prices){
int n=prices.size();
int k=2; // k conosidered to be the cooldown period
int dp[100002][2];
memset(dp,0,sizeof(dp));
for(int i=n-1;i>=0;i--){
for(int buy=0;buy<2;buy++){
if(buy==0){
dp[i][buy]=max(dp[i+1][0],-prices[i]+dp[i+1][1]);
}else{
dp[i][buy]=max(dp[i+1][1],+prices[i]+dp[i+k][0]);
}
}
}
return dp[0][0];
}