-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxStockProfit.java
More file actions
225 lines (197 loc) · 4.87 KB
/
MaxStockProfit.java
File metadata and controls
225 lines (197 loc) · 4.87 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package codingInterview;
public class MaxStockProfit {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] stock = { 3, 1, 3, 8, 9, 10 };
// int profit = maxProfit1(stock);
// System.out.println(profit);
// int profit = maxProfit2(stock);
// System.out.println(profit);
// int profit = maxProfit3(stock);
// System.out.println(profit);
// int profit = maxProfit4(stock);
// System.out.println(profit);
int k = 3;
// int profit = maxProfit5(k, stock);
// System.out.println(profit);
int profit = maxProfit6(k, stock);
System.out.println(profit);
}
/**
* complete at most k transactions
* 1D dynamic programming
* @param k
* @param stock
* @return
*/
private static int maxProfit6(int k, int[] stock) {
// TODO Auto-generated method stub
if (k <= 0 || stock == null || stock.length < 2) {
return 0;
}
int len = stock.length;
int[] local = new int[k + 1];
int[] global = new int[k + 1];
for (int i = 0; i < len - 1; i++) {
int diff = stock[i + 1] - stock[i];
for (int j = k; j > 0; j--) {
local[j] = Math.max(local[j] + diff, global[j - 1] + Math.max(0, diff));
global[j] = Math.max(local[j], global[j]);
}
}
return global[k];
}
/**
* complete at most k transactions
* 2D dynamic programming
* @param k
* @param stock
* @return
*/
private static int maxProfit5(int k, int[] stock) {
// TODO Auto-generated method stub
if (k <= 0 || stock == null || stock.length < 2) {
return 0;
}
int len = stock.length;
// The local array tracks maximum profit of j transactions
// and the last transaction is on ith day.
int[][] local = new int[len][k + 1];
// The global array tracks the maximum profit of j transactions until ith day.
int[][] global = new int[len][k + 1];
for (int i = 1; i < len; i++) {
int diff = stock[i] - stock[i - 1];
for (int j = 1; j <= k; j++) {
local[i][j] = Math.max(local[i - 1][j] + diff,
global[i - 1][j - 1] + Math.max(diff, 0));
global[i][j] = Math.max(local[i][j], global[i - 1][j]);
}
}
return global[len - 1][k];
}
/**
* complete at most 2 transactions divide and conquer
*
* @param stock
* @return
*/
private static int maxProfit4(int[] stock) {
// TODO Auto-generated method stub
if (stock == null || stock.length < 2) {
return 0;
}
int profit = 0;
int[] left = new int[stock.length];
int[] right = new int[stock.length];
maxProfit4(stock, 1, stock.length, left, right, profit);
printArray(left);
printArray(right);
for (int i = 0; i < stock.length; i++) {
if (profit < left[i] + right[i]) {
profit = left[i] + right[i];
}
}
return profit;
}
/**
* helper function for maxProfit4
*
* @param stock
* @param start
* @param length
* @param right
* @param left
* @param profit
*/
private static void maxProfit4(int[] stock, int start, int length,
int[] left, int[] right, int profit) {
// TODO Auto-generated method stub
for (int i = start; i < length; i++) {
if (stock[i] > stock[i - 1]) {
left[i] = left[i - 1] + stock[i] - stock[i - 1];
}
if (stock[length - i - 1] < stock[length - i]) {
right[length - i - 1] = right[length - i] + stock[length - i]
- stock[length - i - 1];
}
}
}
/**
* utility function
*
* @param left
*/
private static void printArray(int[] arr) {
// TODO Auto-generated method stub
if (arr == null || arr.length == 0) {
System.out.println("null");
}
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1) {
System.out.print(arr[i] + " ");
} else {
System.out.println(arr[i]);
}
}
}
/**
* complete as many as one likes
*
* @param stock
* @return
*/
private static int maxProfit3(int[] stock) {
// TODO Auto-generated method stub
if (stock == null || stock.length < 2) {
return 0;
}
int profit = 0;
for (int i = 1; i < stock.length; i++) {
int diff = stock[i] - stock[i - 1];
if (diff > 0) {
profit += diff;
}
}
return profit;
}
/**
* complete at most one transaction
*
* @param stock
* @return
*/
private static int maxProfit2(int[] stock) {
// TODO Auto-generated method stub
if (stock == null || stock.length < 2) {
return 0;
}
int profit = 0;
int minElem = Integer.MAX_VALUE;
for (int i = 0; i < stock.length; i++) {
profit = Math.max(profit, stock[i] - minElem);
minElem = Math.min(minElem, stock[i]);
}
return profit;
}
/**
* complete at most one transaction
*
* @param stock
* @return
*/
private static int maxProfit1(int[] stock) {
// TODO Auto-generated method stub
if (stock == null || stock.length < 2) {
return 0;
}
int profit = Integer.MIN_VALUE;
for (int i = 0; i < stock.length - 1; i++) {
for (int j = 0; j < stock.length; j++) {
if (profit < stock[j] - stock[i]) {
profit = stock[j] - stock[i];
}
}
}
return profit;
}
}