BugForge
DSA

Days Until a Higher Price

Mediummonotonic stack 00:00

For each day, return how many days you must wait for a strictly higher price. Use 0 if it never happens.

Signature

daysUntilHigher(prices: int[]) → int[]
Example 1
prices = [73,74,75,71,69,72,76,73]
output = [1,1,4,2,1,1,0,0]
[1,1,4,2,1,1,0,0]
Example 2
prices = [30,40,50,60]
output = [1,1,1,0]

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ prices[i] ≤ 10^5
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).