-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyAndSellStock3.java
More file actions
32 lines (25 loc) · 874 Bytes
/
BuyAndSellStock3.java
File metadata and controls
32 lines (25 loc) · 874 Bytes
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
package Array;
/**
* The third and one of the important variant uses dp.
*/
public class BuyAndSellStock3 {
public static int maxProfit(int[] price) {
int n = price.length;
int[] profit = new int[n];
// Get the maximum profit with only one transaction allowed.
int maxPrice = price[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (price[i] > maxPrice)
maxPrice = price[i];
profit[i] = Math.max(profit[i + 1], maxPrice - price[i]);
}
// Get the maximum profit with two transactions allowed.
int minPrice = price[0];
for (int i = 1; i < n; i++) {
if (price[i] < minPrice)
minPrice = price[i];
profit[i] = Math.max(profit[i - 1], profit[i] + (price[i] - minPrice));
}
return profit[n - 1];
}
}