Skip to main content

Daily Temperatures

Given an array of daily temperatures Return an array where answer[i] is the number of days to wait for a warmer temperature If there's no future warmer day, answer[i] = 0 Example: [73,74,75,71,69,72,76,73] => [1,1,4,2,1,1,0,0]

Given an array of integer daily temperatures, return the number of days from each index to the first later day with a qualifying temperature. Part 1 returns an integer array for strictly warmer days, and Part 2 returns an integer array for strictly colder days. Part 3 returns a pair [warmerDays, colderDays] for each day. Use 0 when the corresponding future day does not exist; equal temperatures do not qualify.

Examples

You write

daily_temperatures(temperatures: List[int])

  1. Example 1

    Input
    temperatures = [73,74,75,71,69,72,76,73]
    returns
    [1,1,4,2,1,1,0,0]
  2. Example 2

    Input
    temperatures = [30,40,50,60]
    returns
    [1,1,1,0]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Stack

I reach for it when

  • Things must close in the reverse order they opened.
  • You need the nearest earlier or later item that is bigger or smaller.

Say this before you type. Say what each item on the stack is waiting for, and what lets you pop it.

All 17 Stack problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Daily Temperaturesmedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.