Days until warmer
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- 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
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.
Example 2
- Input
- temps = [5, 6, 7]
- Output
- [1, 1, 0]
Each day is followed straight away by a warmer one, except the last.
Example 3
- Input
- temps = [8, 8, 8]
- Output
- [0, 0, 0]
The same temperature isn't warmer.
Rules
tempshas 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.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
For every day, we want the number of days until a strictly warmer day. If none ever comes, that day's answer is 0. The answer is a list with one number per day.
2. Solve an example by hand
[12, 10, 14, 13, 11, 16, 9]. Day 0 is 12°: day 1 is colder, day 2 is 14°, warmer, so day 0 waits 2. Day 1 (10°) waits 1. Day 2 (14°) waits until 16° on day 5: 3 days. And so on.
Look at it another way: when day 2's 14° arrives, it answers two days at once, day 1 and day 0, both still waiting and both colder.
3. The slow way
For each day, look ahead until a warmer one:
return temps.map((t, i) => { for (let j = i + 1; j < temps.length; j++) if (temps[j] > t) return j - i; return 0;});Fine for warm, bouncy weather. But if it keeps getting colder for 100,000 days, each day looks at almost everything after it: O(n²), about 5 billion steps.
4. Spot the pattern
"For each item, the next bigger item to its right" is the signal for a monotonic stack: a stack of items still waiting for their answer.
Why a stack? The days still waiting always get colder from oldest to newest: if a newer day were warmer, it would already have answered the older one. So when a warm day arrives, it checks the newest waiting day first, and keeps popping while they're colder. As soon as it meets a waiting day that's not colder, it can stop: everything below is even warmer.
5. The stack idea
For each day:
- While the day on top of the stack is colder than today, pop it: its wait is
today − that day. - Push today: it's now waiting too.
Days still on the stack at the end never warmed up, and keep their 0.
A stack of days still waiting
temps
waiting (days) (stack, top first)
- empty
waits
function daysUntilWarmer(temps) { const waits = new Array(temps.length).fill(0); const waiting = []; // positions of days with no warmer day yet for (let day = 0; day < temps.length; day++) { // Today is the answer for every colder day still waiting. while (waiting.length > 0 && temps[waiting[waiting.length - 1]] < temps[day]) { const colderDay = waiting.pop(); waits[colderDay] = day - colderDay; } waiting.push(day); } return waits; // days still waiting at the end keep their 0}Every wait starts at 0. No days are waiting yet.
6. The code, line by line
const waits = new Array(temps.length).fill(0); const waiting = []; // positions of days with no warmer day yetwaitsstarts as all zeros, which is the right answer for days that never warm up.waitingholds positions, not temperatures, because we need the position to work out the wait.for (let day = 0; day < temps.length; day++) { // Today is the answer for every colder day still waiting. while (waiting.length > 0 && temps[waiting[waiting.length - 1]] < temps[day]) { const colderDay = waiting.pop(); waits[colderDay] = day - colderDay; }Today answers every colder day on top of the stack.
temps[waiting[waiting.length - 1]]is the temperature of the newest waiting day.waiting.push(day);Then today joins the waiting days.
return waits; // days still waiting at the end keep their 0Anything still waiting keeps its 0.
7. How fast is it?
| Look ahead | Monotonic stack | |
|---|---|---|
| Time | O(n²) | O(n): each day is pushed once and popped at most once |
| Extra memory | O(1) | O(n) for the stack |
Common mistakes
- Storing temperatures on the stack. You also need the day's position to compute the wait; store the position and look up its temperature.
- Popping when the temperature is equal. The same temperature isn't warmer. Use
<, not<=.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Days until warmer
Read the 12 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function daysUntilWarmer(temps) { const waits = new Array(temps.length).fill(0); const waiting = []; // positions of days with no warmer day yet for (let day = 0; day < temps.length; day++) { // Today is the answer for every colder day still waiting. while (waiting.length > 0 && temps[waiting[waiting.length - 1]] < temps[day]) { const colderDay = waiting.pop(); waits[colderDay] = day - colderDay; } waiting.push(day); } return waits; // days still waiting at the end keep their 0}