forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (37 loc) · 1.11 KB
/
Solution.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
class Solution {
public int maxProfit(int[] prices, int k) {
if (prices == null || prices.length <= 1) return 0;
Stack<Integer> buy = new Stack<>();
Stack<Integer> sell = new Stack<>();
for (int p : prices) {
if (buy.empty()) {
buy.push(p);
} else if (buy.size() == sell.size()) {
if (p > sell.peek()) {
sell.pop();
sell.push(p);
} else if (p < sell.peek() - k) {
buy.push(p);
}
} else { // buy.size () > sell.size()
if (p - k > buy.peek()) {
sell.push(p);
} else if (p < buy.peek()) {
buy.pop();
buy.push(p);
}
}
}
if (buy.size() > sell.size()) {
buy.pop();
}
int res = 0;
while (!buy.empty()) {
int b = buy.pop();
int s = sell.pop();
// if (s - b - k > 0)
res += (s - b - k);
}
return res;
}
}