We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7225173 commit fa6e237Copy full SHA for fa6e237
problems/0714.买卖股票的最佳时机含手续费.md
@@ -216,7 +216,30 @@ class Solution: # 贪心思路
216
```
217
218
Go:
219
-
+```golang
220
+func maxProfit(prices []int, fee int) int {
221
+ var minBuy int = prices[0] //第一天买入
222
+ var res int
223
+ for i:=0;i<len(prices);i++{
224
+ //如果当前价格小于最低价,则在此处买入
225
+ if prices[i]<minBuy{
226
+ minBuy=prices[i]
227
+ }
228
+ //如果以当前价格卖出亏本,则不卖,继续找下一个可卖点
229
+ if prices[i]>=minBuy&&prices[i]-fee-minBuy<=0{
230
+ continue
231
232
+ //可以售卖了
233
+ if prices[i]>minBuy+fee{
234
+ //累加每天的收益
235
+ res+=prices[i]-minBuy-fee
236
+ //更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!)
237
+ minBuy=prices[i]-fee
238
239
240
+ return res
241
+}
242
+```
243
Javascript:
244
```Javascript
245
// 贪心思路
0 commit comments