-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
33 lines (29 loc) · 808 Bytes
/
solution.js
File metadata and controls
33 lines (29 loc) · 808 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
31
32
33
/**
* @param {number[]} prices
* @param {number} fee
* @return {number}
*/
var maxProfit = function(prices, fee) {
if (prices.length == 0)
return 0
let maxProfit = 0,
holder = prices[0],
maxPrice = prices[0]
for (let i = 1; i < prices.length; i++) {
let 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
};