★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-vyvjoakk-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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.)github
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:
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Note:spa
0 < prices.length <= 50000
.0 < prices[i] < 50000
.0 <= fee < 50000
.給定一個整數數組 prices
,其中第 i
個元素表明了第 i
天的股票價格 ;非負整數 fee
表明了交易股票的手續費用。code
你能夠無限次地完成交易,可是你每次交易都須要付手續費。若是你已經購買了一個股票,在賣出它以前你就不能再繼續購買股票了。htm
返回得到利潤的最大值。blog
示例 1:element
輸入: 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
.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 }