Skip to the code
+25 XP

#263 Days Until a Higher Price

medium

A shopping app's price-tracking chart shows, under each day, how many days you would have had to wait for a strictly higher price. prices[i] is the price on day i.

Write daysToWait(prices) that returns an array where position i is the number of days from day i to the next day with a higher price, or 0 if no later day is higher.

Example 1

Input: prices = [60,64,63,58,57,61,68,64]
Output: [1,5,4,2,1,1,0,0]

Day 1 (64) waits 5 days for 68; day 3 (58) waits 2 days for 61.

Example 2

Input: prices = [30,40,50,60]
Output: [1,1,1,0]

Every day is followed by a higher one.

Constraints

  • 1 <= prices.length <= 2 * 10^5
  • 1 <= prices[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).