• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode714.买卖股票的最佳时机含手续费|BestTimetoBuyandSellStockwithTrans ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode1106.解析布尔表达式|ParsingABooleanExpression发布时间:2022-07-13
下一篇:
swift中的nil与Objective-C中的nil区别发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap