Loading…
Loading…
Given an array of daily stock prices, return the maximum profit from buying on one day and selling on a later day. Print 0 if no profit is possible.
Input: 7 1 5 3 6 4
Output: 5 (buy at 1, sell at 6)
Track the lowest price seen so far while scanning left to right. At each day, the best possible profit if you sold today is today's price minus the lowest price seen before today — track the running maximum of that quantity as you go. One pass, O(1) extra space.
Line 1: daily prices, space-separated
Print the maximum profit.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.