Ctrl K

Single Trade Profit

Easy
You know the price of a rare coin for the coming days: rates[i] is its price on day i. You may buy the coin once and sell it once on a later day. Return the largest profit (selling price minus buying price) you can make. If no profit is possible, return 0.
Example 1
rates=[12, 15, 4, 9, 7, 11] → 7

The best move is to buy on the day the rate is 4 and sell when it is 11: 11 − 4 = 7. Buying at 12 and selling at 15 earns only 3.

Example 2
rates=[9, 9, 5, 2] → 0

The rate only falls or stays flat, so no profit is possible and the answer is 0.

Example 3
rates=[3] → 0

With a single day you cannot buy and sell on different days.

Constraints
  • 1 ≤ rates.length ≤ 10⁵
  • 0 ≤ rates[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRows of the Number Pyramid EasyA Single Row of the Number Pyramid EasyCheapest Climb to the Top EasyAlternating Team Picks EasyWater Between Walls Hard
loading Python…
Input
rates=[12, 15, 4, 9, 7, 11]
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.