Show the best price still to come

Write codeEasy10 min

Your task

A pricing dashboard shows, next to each day in a price history, the best price still to come.

Write best_price_ahead(prices) that returns, for each day, the highest price on any later day.

Input and output:

  • prices: a list of 0 to 100,000 daily prices, each an integer from 0 to 1,000,000.
  • Returns a list of integers of the same length, one per day, in the same order.

Rules:

  • The value for day i is the largest price among the days after day i. Day i itself does not count.
  • The last day has no later days, so its value is -1.
  • An empty list returns an empty list.

Examples

best_price_ahead(prices: list[int]) -> list[int]
Example 1
Inputbest_price_ahead([3, 7, 1, 4])
Output[7, 4, 4, -1]
Example 2
Inputbest_price_ahead([5, 5, 2, 8, 0])
Output[8, 8, 8, 0, -1]