Você está na página 1de 3

2017/11/4 Best Time to Buy and Sell Stock with Transaction Fee - LeetCode Articles

Articles 714. Best Time to Buy and Sell Stock with Transaction Fee

Previous (/articles/falling-squares/) Next (/articles/minimum-ascii-delete-sum-for-two-strings/)

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/)

Average Rating: 5 (2 votes)

Oct. 21, 2017

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 prot 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 .

Approach #1: Dynamic Programming [Accepted]

Approach #1: Dynamic Programming [Accepted]

Intuition and Algorithm

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

Time Complexity: O(N ), where N is the number of prices.

Space Complexity: O(1), the space used by cash and hold .

Analysis written by: @awice (https://leetcode.com/awice).

Previous (/articles/falling-squares/) Next (/articles/minimum-ascii-delete-sum-for-two-strings/)

Join the conversation

Login to Reply

leibo435890024 commented last week

This is awesome! Thank you for sharing.


iscuss.leetcode.com/user/leibo435890024)

VenomSnakeTPP commented last week

Could u tell clearly about your solution ?


iscuss.leetcode.com/user/venomsnaketpp)

kenan3 commented last week

@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!

benjamin19890721 commented last week

@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/)

sha256pki commented last week

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

immiao commented last week

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 .

junxuelian commented last week

Can you explain your solution?Thanks!


iscuss.leetcode.com/user/junxuelian)

kenan3 commented last week

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.

View original thread (https://discuss.leetcode.com/topic/107955) Load more comments...

Copyright 2017 LeetCode


Contact Us | Frequently Asked Questions (/faq/) | Terms of Service (/tos/)

https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ 3/3

Você também pode gostar