Ctrl K

Biggest Later Rise

Easy
The array prices holds a stock's price day by day: prices[i] is the price on day i. Pick two days i < j with prices[i] < prices[j] so that prices[j] - prices[i] is as large as possible, and return that difference. If the price never climbs above any earlier price, return -1.
Example 1
prices=[6, 2, 9, 4, 11] → 9

Day 1 (price 2) and day 4 (price 11) give a rise of 9. The pair 6 → 11 gives only 5.

Example 2
prices=[8, 8, 5, 3] → -1

The price never goes up, and equal prices do not count as a rise. The answer is -1.

Example 3
prices=[10, 1, 3] → 2

10 is above every later price, so only the pair 1 → 3 works: a rise of 2.

Constraints
  • 2 ≤ len(prices) ≤ 10⁵
  • 1 ≤ prices[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
prices=[6, 2, 9, 4, 11]
Expected
9

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