1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| /**
* hold: the money status when holding the stock.
* sold: the money status after selling the stock.
*/
class Solution {
public int maxProfit(int[] prices, int fee) {
int hold = -prices[0], sold = 0;
for (int price:prices) {
int pre_hold = hold; // memorize hold in previous day to calculate the sold today.
hold = Math.max(hold, sold - price);
sold = Math.max(sold, pre_hold + price - fee);
}
return sold;
}
}
|