From 09a19372ba0e805e0a5a776bd64434f26f3f72af Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 22:11:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=B0=8F=E5=BF=83=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0714=EF=BC=88=E8=B4=AA=E5=BF=83=EF=BC=89=E5=93=AA?= =?UTF-8?q?=E9=87=8C=E5=8E=BB=E4=BA=86=EF=BC=8C=E7=8E=B0=E5=9C=A8=E6=8A=8A?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84=E4=BC=98=E5=8C=96=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E4=BB=A3=E7=A0=81=E9=87=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=BF=9B=E4=BA=86=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=E4=B8=AD=EF=BC=8C=E5=B7=B2=E6=9B=B4=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14.买卖股票的最佳时机含手续费.md | 16 ---------------- ...最佳时机含手续费(动态规划).md | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 964d5ea9..f7ddeaf7 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -195,22 +195,6 @@ class Solution { // 动态规划 } ``` -```java -// 一维数组优化 -class Solution { - public int maxProfit(int[] prices, int fee) { - int[] dp = new int[2]; - dp[0] = -prices[0]; - dp[1] = 0; - for (int i = 1; i <= prices.length; i++) { - dp[0] = Math.max(dp[0], dp[1] - prices[i - 1]); - dp[1] = Math.max(dp[1], dp[0] + prices[i - 1] - fee); - } - return dp[1]; - } -} -``` - Python: diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 2b3416d4..700b8cde 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -134,6 +134,20 @@ public int maxProfit(int[] prices, int fee) { } return Math.max(dp[len - 1][0], dp[len - 1][1]); } + +// 一维数组优化 +class Solution { + public int maxProfit(int[] prices, int fee) { + int[] dp = new int[2]; + dp[0] = -prices[0]; + dp[1] = 0; + for (int i = 1; i <= prices.length; i++) { + dp[0] = Math.max(dp[0], dp[1] - prices[i - 1]); + dp[1] = Math.max(dp[1], dp[0] + prices[i - 1] - fee); + } + return dp[1]; + } +} ``` Python: