add js solution for maxProfit with fee

This commit is contained in:
Qi Jia 2021-07-09 22:13:19 -07:00 committed by GitHub
parent a9344c2f94
commit 1c583ae1df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -153,7 +153,18 @@ class Solution:
Go Go
Javascript
```javascript
const maxProfit5 = (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]);
}
```
----------------------- -----------------------