-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbestTimeStocks.java
61 lines (48 loc) · 1.37 KB
/
bestTimeStocks.java
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
import java.util.*;
public class bestTimeStocks {
public int maxProfitBuyOnce(final List<Integer> a) {
//If you were only permitted to complete at most one transaction Restriction. Must buy before selling
int cur = 0, max = 0;
for (int i = 1; i < a.size(); i++){
cur = a.get(i)- a.get(i- 1) + Math.max(cur, 0);
max = Math.max(max, cur);
}
return max;
}
public int maxProfitMultiple(final List<Integer> a) {
//Buy and sell multiple times. Buy before selling. One at hand always
int n = a.size();
if (n <= 1)
return 0;
int T [] = new int[n];
T[0] = 0;
for (int i = 1; i < n; i++){
if (a.get(i) > a.get(i -1))
T[i] = a.get(i) - a.get(i -1) + T[i -1];
else
T[i] = T[i -1];
}
return T[n -1];
}
public int maxProfit(final List<Integer> a) {
//At most 2 transactions. Replace by k
int n = a.size();
int A[] = new int[n];
for (int i = 0; i < n; i++)
A[i] = a.get(i);
if (n <= 1)
return 0;
int T[][] = new int[2 + 1][n];
for (int i = 1; i <= 2; i++){
int maxDiff = -A[0];
for (int j = 1; j < n; j++){
T[i][j] = Math.max(T[i][j -1], A[j] + maxDiff);
maxDiff = Math.max(maxDiff, T[i -1][j] - A[j]);
}
}
return T[2][n -1];
}
public static void main(String[] args) {
System.out.println(new bestTimeStocks().maxProfit(Arrays.asList(2, 5, 7 ,1, 4, 3, 1, 3)));
}
}