Example 1
Input
prices = [7, 1, 5, 3, 6, 4]
Output
5
Loading CodeSprint...
/problems/best-time-to-trade-stock
A list records one stock price per day. Choose one day to buy and a later day to sell, then return the largest profit available. Return 0 when no profitable trade exists.
Implement this function and return the result. CodeSprint supplies each test case automatically.
maxProfit(prices) → numberInput
prices = [7, 1, 5, 3, 6, 4]
Output
5
Input
prices = [7, 6, 4, 3, 1]
Output
0
Constraints
1 <= prices.length <= 100,000 0 <= prices[i] <= 1,000,000