Best Time to Buy and Sell Stock
Find the best profit from one buy and one later sell
DSA, Easy, Sliding Window
Which structure or algorithm, and what does it cost.
A list prices holds the price of one stock on each day, in order. You may buy on one day and sell on a later day, once. Return the largest profit you can make. If every possible trade loses money, make no trade and return 0.
Examples
Constraints
- 1 <= length of
prices<= 100,000. - Each price is a whole number between 0 and 1,000,000.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Fix the day you sell. Which earlier day would be the best one to have bought on?
Hint 2 the approach
For a given sell day, the best buy day is the cheapest day before it. You can keep that cheapest price as you walk the list.
Hint 3 the key step
Walk the days once. Keep the lowest price seen so far and the best profit so far. On each day, try selling at that price, then update the lowest.
Walkthrough
Full walkthrough brute force to best
Brute force
Try every pair of a buy day and a later sell day, and keep the largest difference, or 0 if none is positive. That is O(n^2) time and O(1) extra memory. For 100,000 days it is about 5 billion pairs, too slow.
Better method
For each sell day, the best buy day is the cheapest day before it. Build a list that holds the lowest price up to each day, then take the largest gap between a price and the lowest price before it. This is O(n) time, but it needs O(n) extra memory for the list.
Best method
Fix the sell day as today. The best buy day for it is the cheapest day before it. Walk the days once. Keep lowest, the cheapest price seen so far, and best, the largest profit seen so far. On each day, selling today after buying at lowest earns price minus lowest, so update best with it. Then update lowest. This is correct because for any sell day the best buy day is the cheapest day before it, and lowest is exactly that price. Every sell day is tried with its best buy day, so no trade is missed. best starts at 0, which stands for making no trade.
Hand trace
Run on [9, 4, 12, 7, 15, 3]. Start with lowest 9 and best 0. Price 4: profit -5, best stays 0, lowest becomes 4. Price 12: profit 8, best 8. Price 7: profit 3, best stays 8. Price 15: profit 11, best 11. Price 3: profit -1, best stays 11, lowest becomes 3. The result is 11.
Code
def max_profit(prices):
lowest = prices[0]
best = 0
for p in prices:
best = max(best, p - lowest)
lowest = min(lowest, p)
return bestTime and memory
O(n) time, because each price is visited once. O(1) memory, because only two numbers are kept.
Common mistakes
- Using the lowest and the highest price of the whole list. For [20, 3, 18, 1, 2] that gives 19, but the high comes before the low, so it is not a trade. The answer is 15.
- Returning a negative number when prices only fall. Start
bestat 0, so making no trade gives 0. - Searching back for the lowest price on every day. That repeats work and makes the method O(n^2).