From 3cff2bbe8334b5a23448674d3f2643650f28146d Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:10:06 -0700 Subject: [PATCH 1/3] add js solution for maxProfit IV --- .../0188.买卖股票的最佳时机IV.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 431c292b..8d9c8657 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -230,6 +230,31 @@ 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]; +}; +``` + ----------------------- From 633de2413cb2570cde71ad4091152c86f2672da1 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:10:53 -0700 Subject: [PATCH 2/3] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 8d9c8657..0470e528 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -256,7 +256,6 @@ const maxProfit = (k,prices) => { ``` - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 486fff2d02e41337928d109b612c0fa1487ffe30 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:11:13 -0700 Subject: [PATCH 3/3] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 0470e528..c34a10b7 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -255,7 +255,6 @@ const maxProfit = (k,prices) => { }; ``` - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)