Best Time to Buy and Sell Stock
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write maxProfit(prices): prices[i] is a share's price on day i. Buy on one day, sell on a later day, and return the largest profit you can make, or 0 if no trade makes money (including when there are fewer than two days). For example, maxProfit([7, 1, 5, 3, 6, 4]) is 5: buy at 1 and sell at 6. The checks include 100,000 prices, so trying every pair of days is too slow.
Your code must pass
- Finds the best buy and later sell
- Never sells before buying
- Returns 0 when no trade makes money
- Is fast enough for 100,000 prices