MediumArrayStackMonotonic Stack

Daily Temperatures

LeetCode
1 approach, code in all languages

You are given an array `temperatures` where `temperatures[i]` is the temperature recorded on day `i`.

For each day, determine how many days you must wait until a warmer temperature occurs. If no future day is warmer, the answer for that day is `0`.

Return an array `answer` such that `answer[i]` is the number of days to wait after day `i` for a warmer temperature.

Example 1

Input: temperatures = [73,74,75,71,69,72,76,73]

Output: [1,1,4,2,1,1,0,0]

Day 0 (73) warms up the next day (74), so 1. Day 2 (75) waits 4 days for 76. The last two days never see anything warmer, so 0.

Example 2

Input: temperatures = [30,40,50,60]

Output: [1,1,1,0]

Each day is immediately followed by a warmer one except the final day, which has no warmer future day.

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now