InferenceAI

Enter at least two characters to search.

Start free

Best Single Trade

easySliding Window15 min10 XP

prices[i] is a stock's price on day ii. Choose one day to buy and a later day to sell. Return the maximum profit, or 0 if no profit is possible.

Examples

EXAMPLE WALKTHROUGHbuy low sell high

A guided view of the published example, not a trace of your code.

INPUT[[7,1,5,3,6,4]]
TASKbest_single_trade()
EXPECTED OUTPUT5

Start with the input values shown in this example.

Example 1 of 3

Input: prices = [7,1,5,3,6,4]

Output: 5

Explanation: buy low sell high

Input: prices = [7,6,4,3,1]

Output: 0

Explanation: falling prices

Input: prices = [2]

Output: 0

Explanation: one day

Hint 1

Track the lowest price seen so far as you scan the days.

Topics

DSASliding WindowPython