From bd3d65c41530e2f8268986ce9f4b13d889d5925d Mon Sep 17 00:00:00 2001 From: DoubleYellowIce <65336599+DoubleYellowIce@users.noreply.github.com> Date: Thu, 19 Aug 2021 14:34:15 +0800 Subject: [PATCH] =?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 --- .../0188.买卖股票的最佳时机IV.md | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 9fe7a919..66676a7a 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -196,7 +196,7 @@ class Solution { } } -// 版本二: 空间优化 +// 版本二: 二维 dp数组 class Solution { public int maxProfit(int k, int[] prices) { if (prices.length == 0) return 0; @@ -220,6 +220,25 @@ class Solution { return dp[len - 1][k*2]; } } + +//版本三:一维 dp数组 +class Solution { + public int maxProfit(int k, int[] prices) { + //在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况 + if(prices.length==0)return 0; + int[] dp=new int[2*k+1]; + for (int i = 1; i <2*k ; i+=2) { + dp[i]=-prices[0]; + } + for (int i = 0; i