-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path309.best-time-to-buy-and-sell-stock-with-cooldown.cpp
68 lines (63 loc) · 1.81 KB
/
309.best-time-to-buy-and-sell-stock-with-cooldown.cpp
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
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode id=309 lang=cpp
*
* [309] Best Time to Buy and Sell Stock with Cooldown
*
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
*
* algorithms
* Medium (44.41%)
* Total Accepted: 97.4K
* Total Submissions: 219.2K
* Testcase Example: '[1,2,3,0,2]'
*
* Say you have an array for which the ith element is the price of a given
* stock on day i.
*
* Design an algorithm to find the maximum profit. You may complete as many
* transactions as you like (ie, buy one and sell one share of the stock
* multiple times) with the following restrictions:
*
*
* You may not engage in multiple transactions at the same time (ie, you must
* sell the stock before you buy again).
* After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1
* day)
*
*
* Example:
*
*
* Input: [1,2,3,0,2]
* Output: 3
* Explanation: transactions = [buy, sell, cooldown, buy, sell]
*
*/
class Solution {
public:
unordered_map<int, unordered_map<int, int>> mm;
int callme(int i, int state, vector<int>& prices){
if(i == (int)prices.size())
return 0;
if(mm.find(i)!=mm.end() && mm[i].find(state)!=mm[i].end())
return mm[i][state];
if(i == (int)prices.size()-1)
if(state == 2)
return prices[i] + callme(i+1, 3, prices);
else
return 0;
int ans;
if(state == 1)
ans = max(callme(i+1, 1, prices), - prices[i]+callme(i+1, 2, prices));
else if(state == 2)
ans = max(callme(i+1, 2, prices), +prices[i] + callme(i+1, 3, prices));
else if(state == 3)
ans = callme(i+1, 1, prices);
mm[i][state] = ans;
return ans;
}
int maxProfit(vector<int>& prices) {
mm.clear();
return callme(0, 1, prices);
}
};