mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
add js solution for maxProfit with fee
This commit is contained in:
@ -153,7 +153,18 @@ class Solution:
|
||||
|
||||
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]);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user