714_Best Time to Buy and Sell Stock with Transaction Fee
Your are given an array of integersprices
, for which thei
-th element is the price of a given stock on dayi
; and a non-negative integerfee
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:
Solution
Idea: the idea is similar to that of buy and sell stock with unlimited time. Only the condition when a new transaction starts is different because of the fee. the new condition is :
When the current price is larger than previous maximum price - fee and larger than minimum price, update the maximum price with the maximum of itself and the current price
Otherwise,
When the current price is smaller than the minimum price, start a new transaction
When the current price is smaller than previous maximum price - fee, start a new transaction
Time Complexity:
Space Complexity:
Last updated