Given an array where prices[i] is the price of a stock on day i, choose a single day to buy and a later day to sell to maximize your profit, and return the maximum profit achievable. Return 0 if no profit is possible.
prices = [7,1,5,3,6,4]5prices = [7,6,4,3,1]0Try it now — no sign-up needed. Write your solution in Python or JavaScript, run it against test cases, and submit for a verdict, right in your browser.
Open the editor →A window — a contiguous subarray or substring — expands and shrinks as it moves across the input, so you track a running condition instead of recomputing it from scratch for every possible window.
Read the full Sliding Window guide →
Original problem on LeetCode ↗