ALG-M001MediumGreedy
Best Single Trade
Given prices for consecutive days, buy on one day and sell on a strictly later day to maximize profit. Return 0 if no positive profit exists.
Contract
input prices: integer[]
return integer
Constraints
- • Every price >= 0
- • 0 <= days <= 100000
Examples
Example 1
prices = [7, 1, 5, 3, 6, 4]
→ 5
Example 2
prices = [7, 6, 4, 3, 1]
→ 0
5 judge tests (2 shown above). Target complexity: O(n).