Comic Book Flip
A collector's shop reprices a rare comic every morning, and you have written down the prices for the past few weeks. Had you bought one copy on some day and sold it for the shop's price on a later day, what is the most you could have made?
You buy before you sell, and only once. If every deal loses money, you would not have bought at all.
Your task
Write bestFlip(prices). prices[i] is the price on day i. Return the largest value of prices[sell] - prices[buy] over all pairs of days with buy < sell.
If no pair makes a profit, or there are fewer than two days, return 0.
bestFlip(prices: int[]) → int
Examples
prices = [30, 22, 41, 18, 35, 27]
19
Buying on day 1 at 22 and selling on day 2 at 41 gives a profit of 19. Buying at the lowest price, 18 on day 3, only gives 35 - 18 = 17 at best, so the cheapest day is not always the best day to buy.
prices = [50, 44, 39, 31]
0
The price only falls, so every deal loses money. The best choice is not to trade, and the answer is 0.
prices = [12, 40, 5, 9]
28
The highest price is 40 and the lowest 5, but 5 comes after 40. The best real deal is 40 - 12 = 28.
Constraints
- 0 ≤ prices.length ≤ 100,000
- 0 ≤ prices[i] ≤ 100,000
Start with brute force
Try every buying day with every later selling day and keep the largest difference.
That is about n²/2 pairs: roughly 5 billion for 100,000 days. Each selling day rescans all earlier days for the cheapest, though that answer changes by only one day at a time.
Hints
0 of 4 shown- Hint 1
Pretend you sell on day 4 in the first example, at 35. Which earlier day would you want to have bought on, and why?
- Hint 2
For any selling day, the best buying day is the cheapest day before it. From the past you need just one number.
- Hint 3
Walk the days in order, keeping the lowest price so far. What is the best profit if you sell today?
- Hint 4
Keep
cheapest(lowest price so far) andbest(best profit so far, starting at 0). For each price, first updatebestwithprice - cheapest, then updatecheapestwithprice.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Instead of "which pair of days?", ask for each day: "if I sell today, what is my best profit?" It is today's price minus the cheapest price before today. Carry that cheapest price along and each day costs one step.
Trace [30, 22, 41, 18, 35, 27]:
- On day 0 the price is 30 and there is no earlier day to buy on, so cheapest becomes 30 and best stays 0.
- On day 1 the price is 22. Selling would lose 8, so best stays 0, and cheapest becomes 22.
- On day 2 the price is 41. Selling gives 41 - 22 = 19, so best becomes 19.
- On day 3 the price is 18. Selling would lose 4, so best stays 19, and cheapest becomes 18.
- On day 4 the price is 35. Selling gives 35 - 18 = 17, which is less than 19, so best stays 19.
- On day 5 the price is 27. Selling gives 9, so best stays 19, and the final answer is 19.
Update best before cheapest. The other order lets you buy and sell on the same day, which is harmless here because it gives a profit of 0, but it would be a bug in a problem where same-day trades are not allowed to count.
function bestFlip(prices) {
let cheapest = Infinity; // lowest price on any earlier day
let best = 0; // not trading is always allowed
for (const price of prices) {
best = Math.max(best, price - cheapest); // sell today
cheapest = Math.min(cheapest, price); // or remember today as a buy
}
return best;
}def best_flip(prices):
cheapest = float("inf") # lowest price on any earlier day
best = 0 # not trading is always allowed
for price in prices:
best = max(best, price - cheapest) # sell today
cheapest = min(cheapest, price) # or remember today as a buy
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.