Академический Документы
Профессиональный Документы
Культура Документы
Articles 714. Best Time to Buy and Sell Stock with Transaction Fee
714. Best Time to Buy and Sell Stock with Transaction Fee
(/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
07/265/?return=/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/) (/ratings/107/265/?return=/articles/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.)
Example 1:
Note:
At the end of the i -th day, we maintain cash , the maximum prot we could have if we did not have a share of stock, and hold , the maximum
prot we could have if we owned a share of stock.
To transition from the i -th day to the i+1 -th day, we either sell our stock cash = max(cash, hold + prices[i] - fee) or buy a stock hold
= max(hold, cash - prices[i]) . At the end, we want to return cash . We can transform cash rst without using temporary variables because
selling and buying on the same day can't be better than just continuing to hold the stock.
Python
class Solution(object):
def maxProfit(self, prices, fee):
cash, hold = 0, -prices[0]
for i in range(1, len(prices)):
cash = max(cash, hold + prices[i] - fee)
hold = max(hold, cash - prices[i])
return cash
Java
https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ 1/3
2017/11/4 Best Time to Buy and Sell Stock with Transaction Fee - LeetCode Articles
class Solution {
public int maxProfit(int[] prices, int fee) {
int cash = 0, hold = -prices[0];
for (int i = 1; i < prices.length; i++) {
cash = Math.max(cash, hold + prices[i] - fee);
hold = Math.max(hold, cash - prices[i]);
}
return cash;
}
}
Complexity Analysis
Login to Reply
@benjamin19890721 (https://discuss.leetcode.com/uid/23851) I have completed buy and sell stock 1 and 2, and jump to this
iscuss.leetcode.com/user/kenan3)
question. I found the way to solve this question is very different with those two questions, but it does look similar with buy and
sell stock 3. I will gure it out. Thanks!
@kenan3 (https://discuss.leetcode.com/uid/30241) LeetCode has several problems that are variations of this one. It should be
iscuss.leetcode.com/user/benjamin19890721)
obvious after you complete those:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ (https://leetcode.com/problems/best-time-to-buy-
and-sell-stock/description/)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ (https://leetcode.com/problems/best-time-to-
buy-and-sell-stock-ii/description/)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ (https://leetcode.com/problems/best-time-to-
buy-and-sell-stock-iii/description/)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ (https://leetcode.com/problems/best-time-to-
buy-and-sell-stock-iv/description/)
I think explaining optimal substructure and overlapping sub-problem would help vastly. its also unclear how formulas of cash
iscuss.leetcode.com/user/sha256pki)
and hold are arrived at
https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ 2/3
2017/11/4 Best Time to Buy and Sell Stock with Transaction Fee - LeetCode Articles
I feel this is semantically incorrect. cash has been updated before hold = max(hold, cash - prices[i]) . But luckily if
iscuss.leetcode.com/user/immiao)
cash comes from the previous cash , it's ne. If cash is from hold + prices[i] - fee , then in hold = max(hold, cash
- prices[i]) , cash - prices[i] equals hold + prices[i] - fee - prices[i] which equals hold - fee , which is
always smaller than hold , leading to a correct result. This is tricky and misleading. It would be better to use preCash and
preHold .
Very smart solution! Could you tell me how you came up with this solution? Is there any other resources or questions that can
iscuss.leetcode.com/user/kenan3)
help me build up this knowledge/idea? Thanks.
https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ 3/3