From f77fa05df7f506b33be4efa4eaca07beabc2e725 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 17 Dec 2022 16:26:04 +0800 Subject: [PATCH] =?UTF-8?q?update=200714.=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=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=9A=E4=BF=AE=E6=94=B9markdown?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14.买卖股票的最佳时机含手续费.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index faa56d42..005f9c38 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -217,25 +217,25 @@ class Solution: # 贪心思路 ``` ### Go -```golang +```go func maxProfit(prices []int, fee int) int { var minBuy int = prices[0] //第一天买入 var res int - for i:=0;i=minBuy&&prices[i]-fee-minBuy<=0{ + if prices[i] >= minBuy && prices[i]-fee-minBuy <= 0 { continue } //可以售卖了 - if prices[i]>minBuy+fee{ + if prices[i] > minBuy+fee { //累加每天的收益 - res+=prices[i]-minBuy-fee + res += prices[i]-minBuy-fee //更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!) - minBuy=prices[i]-fee + minBuy = prices[i]-fee } } return res