-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimetoBuyandSellStockwithCooldown.java
More file actions
52 lines (44 loc) · 1.63 KB
/
BestTimetoBuyandSellStockwithCooldown.java
File metadata and controls
52 lines (44 loc) · 1.63 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
/*
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:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
*/
import java.util.*;
public class BestTimetoBuyandSellStockwithCooldown {
public static int maxProfit(int[] prices) {
if(prices.length <= 0) return 0;
int L = prices.length;
int[] buy = new int[L];
int[] sell = new int[L];
int[] cooldown = new int[L];
buy[0] = -prices[0];
sell[0] = 0;
cooldown[0] = 0;
for(int i = 1; i <L; i++){
buy[i] = Math.max(cooldown[i-1] - prices[i], buy[i-1]);
sell[i] = Math.max(buy[i-1] + prices[i], sell[i-1]);
cooldown[i] = Math.max(cooldown[i-1], sell[i-1]);
}
if(sell[L-1] > cooldown[L-1]){
return sell[L-1];
}else{
return cooldown[L-1];
}
}
public static void main(String[] args) {
int[] nums1 = new int[] {1, 2, 3, 0, 2};
System.out.println("Array: ");
for(int i=0; i < nums1.length; i++){
System.out.print(nums1[i] + " ");
}
System.out.println();
int res = maxProfit(nums1);
System.out.println("Maximum profit: " + res);
return;
}
}