教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode714. 买卖股票的最佳时机含手续费 | Best Time to Buy and Sell Stock with Transaction Fee

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10506785.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

Example 1:

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
  • Buying at prices[0] = 1
  • Selling at prices[3] = 8
  • Buying at prices[4] = 4
  • Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. 

Note:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

注意:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

Runtime: 796 ms
Memory Usage: 19.8 MB
复制代码
 1 class Solution {
 2     func maxProfit(_ prices: [Int], _ fee: Int) -> Int {
 3         var sold:Int = 0
 4         var hold:Int = -prices[0]
 5         for price in prices
 6         {
 7             var t:Int = sold
 8             sold = max(sold, hold + price - fee)
 9             hold = max(hold, t - price)
10         }
11         return sold
12     }
13 }
复制代码

880ms

复制代码
 1 class Solution {
 2     func maxProfit(_ prices: [Int], _ fee: Int) -> Int {        
 3         var cash = 0, hold = -prices[0]
 4         for i in prices.indices {
 5             cash = max(cash, hold + prices[i] - fee)
 6             hold = max(hold, cash - prices[i])
 7         }
 8         return cash
 9     }
10 
11     func dpMaxProfit(_ prices: [Int], _ fee: Int, _ index: Int, _ end: Int, _ profit: Int, _ buyed: Bool) -> Int {
12 
13         if index >= end {
14             return profit
15         }
16 
17         if buyed {
18             let sum2 = dpMaxProfit(prices, fee, index+1, end, profit, false)
19             let sum3 = dpMaxProfit(prices, fee, index+1, end, profit - fee + prices[index], false)
20             return max(sum2, sum3)
21         }
22         else {
23             let sum1 = dpMaxProfit(prices, fee, index+1, end, profit - prices[index], true)
24             let sum2 = dpMaxProfit(prices, fee, index+1, end, profit, false)
25             return max(sum1, sum2)
26         }
27     }
28 }
复制代码

948ms

复制代码
 1 class Solution {
 2     func maxProfit(_ prices: [Int], _ fee: Int) -> Int {        
 3         var totalProfit = 0, currProfit = 0, buy = 0, sell = 0
 4         for i in 0..<prices.count {
 5             if prices[i] < prices[buy] || prices[sell] - prices[i] > fee {
 6                 buy = i
 7                 sell = i
 8                 totalProfit += currProfit
 9                 currProfit = 0
10             } else if prices[i] > prices[sell] && prices[i] - prices[buy] > fee {
11                 sell = i
12                 currProfit = prices[sell] - prices[buy] - fee
13             }
14         }
15         totalProfit += currProfit
16         return totalProfit
17     }
18 }
复制代码

 

posted @   为敢技术  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°