You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/fishercoder/solutions/_309.java
+57-53Lines changed: 57 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,9 @@
1
1
packagecom.fishercoder.solutions;
2
2
3
-
/**Say you have an array for which the ith element is the price of a given stock on day i.
3
+
/**
4
+
* 309. Best Time to Buy and Sell Stock with Cooldown
5
+
*
6
+
* Say you have an array for which the ith element is the price of a given stock on day i.
4
7
5
8
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:
6
9
@@ -10,54 +13,55 @@ You may not engage in multiple transactions at the same time (ie, you must sell
10
13
11
14
prices = [1, 2, 3, 0, 2]
12
15
maxProfit = 3
13
-
transactions = [buy, sell, cooldown, buy, sell]*/
16
+
transactions = [buy, sell, cooldown, buy, sell]
17
+
*/
14
18
publicclass_309 {
15
-
staticclasssolutionFromProblemAuthor {
19
+
publicstaticclassSolution1 {
16
20
/**
17
21
* The series of problems are typical dp. The key for dp is to find the variables to
18
22
* represent the states and deduce the transition function.
19
-
*
23
+
*
20
24
* Of course one may come up with a O(1) space solution directly, but I think it is better
21
25
* to be generous when you think and be greedy when you implement.
22
-
*
26
+
*
23
27
* The natural states for this problem is the 3 possible transactions : buy, sell, rest.
24
28
* Here rest means no transaction on that day (aka cooldown).
25
-
*
29
+
*
26
30
* Then the transaction sequences can end with any of these three states.
27
-
*
31
+
*
28
32
* For each of them we make an array, buy[n], sell[n] and rest[n].
29
-
*
33
+
*
30
34
* buy[i] means before day i what is the maxProfit for any sequence end with buy.
31
-
*
35
+
*
32
36
* sell[i] means before day i what is the maxProfit for any sequence end with sell.
33
-
*
37
+
*
34
38
* rest[i] means before day i what is the maxProfit for any sequence end with rest.
35
-
*
39
+
*
36
40
* Then we want to deduce the transition functions for buy sell and rest. By definition we
37
41
* have:
38
-
*
42
+
*
39
43
* buy[i] = max(rest[i-1]-price, buy[i-1])
40
-
* sell[i] = max(buy[i-1]+price, sell[i-1])
41
-
* rest[i] = max(sell[i-1], buy[i-1], rest[i-1])
42
-
*
44
+
* sell[i] = max(buy[i-1]+price, sell[i-1])
45
+
* rest[i] = max(sell[i-1], buy[i-1], rest[i-1])
46
+
*
43
47
* Where price is the price of day i. All of these are very straightforward. They simply represents :
44
-
*
45
-
* (1) We have to `rest` before we `buy` and
46
-
* (2) we have to `buy` before we `sell`
48
+
*
49
+
* (1) We have to `rest` before we `buy` and
50
+
* (2) we have to `buy` before we `sell`
47
51
* One tricky point is how do you make sure you sell before you buy, since from the equations it seems that [buy, rest, buy] is entirely possible.
48
-
*
52
+
*
49
53
* Well, the answer lies within the fact that buy[i] <= rest[i] which means rest[i] =
50
54
* max(sell[i-1], rest[i-1]). That made sure [buy, rest, buy] is never occurred.
51
-
*
55
+
*
52
56
* A further observation is that and rest[i] <= sell[i] is also true therefore
53
-
*
57
+
*
54
58
* rest[i] = sell[i-1] Substitute this in to buy[i] we now have 2 functions instead of 3:
55
-
*
59
+
*
56
60
* buy[i] = max(sell[i-2]-price, buy[i-1]) sell[i] = max(buy[i-1]+price, sell[i-1]) This is
57
61
* better than 3, but
58
-
*
62
+
*
59
63
* we can do even better
60
-
*
64
+
*
61
65
* Since states of day i relies only on i-1 and i-2 we can reduce the O(n) space to O(1).
62
66
* And here we are at our final solution:
63
67
*/
@@ -75,49 +79,49 @@ public int maxProfit(int[] prices) {
75
79
returnsell;
76
80
}
77
81
}
78
-
79
-
staticclasssolutionWithTheSecondHighestUpvotes {
82
+
83
+
publicstaticclassSolution2 {
80
84
/**Surprisingly, this solution is even much faster than the one above provided by the author.*/
81
85
/**
82
86
* Here I share my no brainer weapon when it comes to this kind of problems.
83
-
*
87
+
*
84
88
* 1. Define States
85
-
*
89
+
*
86
90
* To represent the decision at index i:
87
-
*
88
-
* buy[i]: Max profit till index i. The series of transaction is ending with a buy.
89
-
* sell[i]: Max profit till index i. The series of transaction is ending with a sell.
90
-
*
91
+
*
92
+
* buy[i]: Max profit till index i. The series of transaction is ending with a buy.
93
+
* sell[i]: Max profit till index i. The series of transaction is ending with a sell.
94
+
*
91
95
* 2. Define Recursion
92
-
*
96
+
*
93
97
* buy[i]: To make a decision whether to buy at i, we either take a rest, by just using the
94
98
* old decision at i - 1, or sell at/before i - 2, then buy at i, We cannot sell at i - 1,
95
-
* then buy at i, because of cooldown.
96
-
* sell[i]: To make a decision whether to sell at i, we either take a rest, by just using the old decision at i - 1,
97
-
* or buy at/before i - 1, then sell at i.
98
-
*
99
+
* then buy at i, because of cooldown.
100
+
* sell[i]: To make a decision whether to sell at i, we either take a rest, by just using the old decision at i - 1,
0 commit comments