Best Time to Buy and Sell Stock
Given an array prices where prices[i] is the price of a stock on day i, choose one day to buy and a later day to sell to maximize profit. Return the maximum profit, or 0 if no profitable transaction is possible.
Open official problem prompt ↗Find the largest increase from an earlier day's price to a later day's price.
Walking forward while remembering the cheapest price you have passed; at each stall you check how much you'd make reselling there against that cheapest memory.
- Input
- prices = [7,1,5,3,6,4]
- Output
- 5
- Why
- Buy on day 1 at price 1 and sell on day 4 at price 6 for a profit of 6 - 1 = 5.
1 <= prices.length <= 10^50 <= prices[i] <= 10^4