Daily Temperatures

MediumArrayMath

Description

Given an array of integers temperatures representing daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day with warmer temperature, keep answer[i] == 0.

Examples

Input:temperatures = [73,74,75,71,69,72,76,73]
Output:[1,1,4,2,1,1,0,0]
Explanation:

For each day, the number of days until a warmer temperature: day 0 (73°) s 1 day for 74°, day 1 (74°) s 1 day for 75°, day 2 (75°) s 4 days for 76°, day 3 (71°) s 2 days for 72°, day 4 (69°) s 1 day for 72°, day 5 (72°) s 2 days for 76°, and days 6-7 have no warmer day ahead.

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

Each day, the next day is warmer except the last.

Input:temperatures = [80,70,65,75,85,60]
Output:[4,2,1,1,0,0]
Explanation:

For day 0 (80°), the next warmer day is day 4 (85°), a gap of 4. For day 1 (70°), day 3 (75°) is warmer, a gap of 2. Day 2 (65°) reaches 75° at day 3, gap of 1. Days with no warmer future day get 0.

Constraints

  • 1 ≤ temperatures.length ≤ 10⁵
  • 30 ≤ temperatures[i] ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!