LeetCode 714 Best Time to Buy and Sell Stock with Transaction Fee
題目
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.
思路
第i天有兩種情況:持有股票及沒有股票。對於持有股票,有兩種操作:賣,或者不賣;對於沒有股票,也有兩種操作:買,或者不買。用sell[i]表示第i天沒有股票(或者執行“賣”操作)所能獲得的最大利潤,buy[i]表示第i天持有股票(或者執行“買”操作)所能獲得的最大利潤,則有:
sell[i] = max(sell[i-1], buy[i-1]+prices[i]-fee)
buy[i] = max(buy[i-1], sell[i-1]-prices[i])
程式碼
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int sell = 0;
int buy = -prices[0];
for(int i = 1; i < prices.size(); i++){
sell=max(sell, buy+prices[i]-fee);
buy=max(buy, sell-prices[i]);
}
return max(sell, buy);
}
};
後
太久沒打演算法題啥都不會,,,想了半天最後還是看了網上解法才略懂了,sad
相關文章
- [leetcode]Best Time to Buy and Sell StockLeetCode
- Leetcode Best Time to Buy and Sell StockLeetCode
- Leetcode Best Time to Buy and Sell Stock IILeetCode
- Best Time to Buy and Sell Stock III -- LeetCodeLeetCode
- Best Time to Buy and Sell Stock leetcode javaLeetCodeJava
- leetcode_best-time-to-buy-and-sell-stock-iiLeetCode
- LeetCode-Best Time to Buy and Sell Stock with CooldownLeetCode
- LeetCode-Best Time to Buy and Sell Stock IVLeetCode
- Leetcode-Best Time to Buy and Sell Stock IIILeetCode
- Best Time to Buy and Sell Stock II leetcode javaLeetCodeJava
- Best Time to Buy and Sell Stock III leetcode javaLeetCodeJava
- Best Time to Buy and Sell Stock系列分析
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- [LeetCode] 121. Best Time to Buy and Sell StockLeetCode
- LeetCode(188) Best Time to Buy and Sell Stock IV (Java)LeetCodeJava
- LeetCode 309. Best Time to Buy and Sell Stock with CooldownLeetCode
- [LeetCode] 122. Best Time to Buy and Sell Stock IILeetCode
- 【Lintcode】393. Best Time to Buy and Sell Stock IV
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode
- 【leetcode】40-best-time-to-buy-and-sell-stock 力扣 121. 買賣股票的最佳時機LeetCode力扣
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳買賣股票時機含冷凍期(Medium)(JAVA)LeetCodeJava
- 42-best-time-to-buy-and-sell-stock-iii 力扣 123. 買賣股票的最佳時機 III力扣
- 44-best-time-to-buy-and-sell-stock-with-cooldown 力扣 309. 買賣股票的最佳時機包含冷凍期力扣
- [LeetCode] 2073. Time Needed to Buy TicketsLeetCode
- iphone 5s cases best buy among the livingiPhone
- The best LeetCode NodesLeetCode
- LeetCode-Best Meeting PointLeetCode
- FEE-Frontiers in Ecology and Evolution
- Lock wait timeout exceeded; try restarting transactionAIREST
- SAP MM Stock Tables and Stock Types
- TTEC遭勒索軟體攻擊後影響客戶業務 包括Verizon、Best Buy、美國銀行等
- 'Lock wait timeout exceeded; try restarting transaction'問題AIREST
- stock Types
- Forget stock
- [LeetCode] Employee Free TimeLeetCode
- Lock wait timeout exceeded; try restarting transaction引數控制AIREST
- OGG Connection test fails with: weblogic.transaction.internal.TimedOutException:AIWebException