Merge pull request #487 from jackeyjia/patch-11

add js solution for maxProfit with fee
This commit is contained in:
程序员Carl
2021-07-14 15:36:20 +08:00
committed by GitHub

View File

@ -153,7 +153,18 @@ class Solution:
Go
Javascript
```javascript
const maxProfit = (prices,fee) => {
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
dp[0][0] = 0 - prices[0];
for (let i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]);
}
return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][1]);
}
```
-----------------------