Skip to content

Commit 1e6a9b8

Browse files
Merge pull request youngyangyang04#487 from jackeyjia/patch-11
add js solution for maxProfit with fee
2 parents c2e3d10 + 69e54d9 commit 1e6a9b8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

problems/0714.买卖股票的最佳时机含手续费(动态规划).md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,18 @@ class Solution:
153153

154154
Go:
155155

156-
156+
Javascript:
157+
```javascript
158+
const maxProfit = (prices,fee) => {
159+
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
160+
dp[0][0] = 0 - prices[0];
161+
for (let i = 1; i < prices.length; i++) {
162+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
163+
dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]);
164+
}
165+
return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][1]);
166+
}
167+
```
157168

158169

159170
-----------------------

0 commit comments

Comments
 (0)