ASAPUtils Logo ASAPUtils
Week 2

Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock in one pass with a running minimum, with C++ and JavaScript solutions, a visualizer, and why this is the gentlest sliding window there is.

The problem

You are given an array where each entry is the price of a stock on that day. Choose one day to buy and a later day to sell to maximise your profit. Return the maximum profit, or 0 if no profitable trade exists.

prices = [7, 1, 5, 3, 6, 4] -> 5 (buy on day 1 at 1, sell on day 4 at 6)

Related Problems