Problem 333396 · easy · Level 03 Linear Management & Searching

Profit If Sold Today

prefix min · running extremum

prices[i] is a share price on day i. For every day i, report the best profit obtainable by buying on some day j <= i and selling on day i (buying and selling on the same day gives 0). Return the list of these per-day profits.

Examples

Input:  prices = [8, 3, 6, 2, 5, 9]
Output: [0, 0, 3, 0, 3, 7]
Explanation: on day 5 the cheapest price so far was 2 (day 3), so 9 - 2 = 7.

Input:  prices = [9, 8, 7]
Output: [0, 0, 0]

Constraints

  • 1 <= len(prices) <= 10**5
  • 0 <= prices[i] <= 10**6
  • Target complexity: O(n) time.

Goals

  • Maintain the running minimum while scanning
  • Produce one output per position
Starting Python…