-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuySellStockIII.java
More file actions
41 lines (34 loc) · 1.04 KB
/
Copy pathBuySellStockIII.java
File metadata and controls
41 lines (34 loc) · 1.04 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
public class BuySellStockIII {
public int maxProfit(int[] prices) {
if(prices.length < 2)
return 0;
int[] leftIndexProfit = new int[prices.length];
int[] rightIndexProfit = new int[prices.length];
int leftMin = prices[0];
for(int i = 1; i < prices.length; i++){
if(prices[i] < leftMin){
leftMin = prices[i];
}
leftIndexProfit[i] = Math.max(leftIndexProfit[i - 1], prices[i] - leftMin);
}
int rightMax = prices[prices.length - 1];
for(int i = prices.length - 2; i >= 0; i--){
if(prices[i] > rightMax){
rightMax = prices[i];
}
rightIndexProfit[i] = Math.max(rightIndexProfit[i + 1], rightMax - prices[i]);
}
int maxProfit = 0;
for(int i = 0; i < prices.length; i++){
if(maxProfit < (leftIndexProfit[i] + rightIndexProfit[i])){
maxProfit = leftIndexProfit[i] + rightIndexProfit[i];
}
}
return maxProfit;
}
public static void main(String[] args) {
int[] prices = new int[]{2,1,2,0,1};
BuySellStockIII stock = new BuySellStockIII();
System.out.println(stock.maxProfit(prices));
}
}