-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (26 loc) · 844 Bytes
/
Solution.java
File metadata and controls
30 lines (26 loc) · 844 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
class Solution {
public int maxProfit(int[] prices, int fee) {
if (prices.length == 0)
return 0;
int maxProfit = 0,
holder = prices[0],
maxPrice = prices[0];
for (int i = 1; i < prices.length; i++) {
int price = prices[i];
if (price > maxPrice)
maxPrice = price;
else if (maxPrice - price >= fee) {
if (maxPrice - holder > fee)
maxProfit += maxPrice - holder - fee;
holder = price;
maxPrice = price;
} else if (price < holder) {
holder = price;
maxPrice = price;
}
}
if (maxPrice - holder > fee)
maxProfit += maxPrice - holder - fee;
return maxProfit;
}
}