Daily Temperatures

Medium#739

You get the temperature of each day in a row. For every day, say how many days you have to wait for a strictly warmer one; if no warmer day ever comes, the answer is 0. Our example: [64, 61, 61, 59, 66, 62, 63] gives [4, 3, 2, 1, 0, 1, 0].

input
temperatures: the temperature of each day, in order.
returns
For every day, how many days later a strictly warmer day comes, or 0 if none ever does.
constraints
  • 1 ≤ temperatures.length ≤ 10⁴
  • 30 ≤ temperatures[i] ≤ 100
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} temperatures
 * @return {number[]}
 */
function dailyTemperatures(temperatures) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more monotonic stack problems

Pattern “Monotonic stack” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.