Skip to content
BytePatterns

Single Stock Trade

EasyArrays#single-pass#running-minimum~15m

Problem

You are given a list of daily prices for one stock, where position i holds the price on day i. You may buy on one day and sell on a strictly later day, at most once. Return the largest profit that single trade can produce. If no later day ever pays more than an earlier one, return 0 because the best move is to skip trading.

Examples

Input:  prices = [8, 3, 6, 1, 9, 4]
Output: 8
Why:    buy on day 3 at 1, sell on day 4 at 9
Input:  prices = [7, 5, 4, 2]
Output: 0
Why:    prices only fall, so every trade would lose money
Input:  prices = [5]
Output: 0
Why:    edge case, a single day leaves no later day to sell on

Hints

0 / 3