Skip to content

Commit

Permalink
Merge pull request youngyangyang04#485 from jackeyjia/patch-9
Browse files Browse the repository at this point in the history
add js solution for  maxProfit IV
  • Loading branch information
youngyangyang04 committed Jul 14, 2021
2 parents 3d41c31 + 486fff2 commit b64df0f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,30 @@ class Solution:
Go:


Javascript:

```javascript
const maxProfit = (k,prices) => {
if (prices == null || prices.length < 2 || k == 0) {
return 0;
}

let dp = Array.from(Array(prices.length), () => Array(2*k+1).fill(0));

for (let j = 1; j < 2 * k; j += 2) {
dp[0][j] = 0 - prices[0];
}

for(let i = 1; i < prices.length; i++) {
for (let j = 0; j < 2 * k; j += 2) {
dp[i][j+1] = Math.max(dp[i-1][j+1], dp[i-1][j] - prices[i]);
dp[i][j+2] = Math.max(dp[i-1][j+2], dp[i-1][j+1] + prices[i]);
}
}

return dp[prices.length - 1][2 * k];
};
```

-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down

0 comments on commit b64df0f

Please sign in to comment.