Skip to content
Algorithms for Beginners

Problems / Stack

Days until warmer

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

temps lists the temperature on each day. For each day, find how many days you'd have to wait for a warmer day (strictly warmer). If no warmer day ever comes, the answer for that day is 0.

Return a list of the waits, one per day.

Examples

  1. Example 1

    Input
    temps = [12, 10, 14, 13, 11, 16, 9]
    Output
    [2, 1, 3, 2, 1, 0, 0]

    Day 0 (12°) waits 2 days for 14°. Day 5 (16°) and day 6 (9°) never see a warmer day.

  2. Example 2

    Input
    temps = [5, 6, 7]
    Output
    [1, 1, 0]

    Each day is followed straight away by a warmer one, except the last.

  3. Example 3

    Input
    temps = [8, 8, 8]
    Output
    [0, 0, 0]

    The same temperature isn't warmer.

Rules

  • temps has between 0 and 100,000 whole numbers, from -50 to 50.
Words on this page, in plain English (1)Open if any word is new to you
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function daysUntilWarmer(temps) {
  // Return a list with one wait per day.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in stack

  • Learn the stack pattern

    Keep the most recent unfinished thing on top, and finish it first.

  • Balanced brackets

    Easy. Check whether every bracket in a string is closed by the right kind, in the right order.

  • Typing with backspace

    Easy. Work out what's on the screen after someone types, including presses of the backspace key.

  • Stack with minimum

    Medium. Build a stack that can also tell you its smallest item instantly.

  • Postfix calculator

    Medium. Work out a sum written with the operators after the numbers, like 3 4 + 2 *.

  • Tidy a file path

    Medium. Turn a messy folder path with dots and extra slashes into its simplest form.