Waiting for a Bigger Wave
A surf school gets an hourly forecast of wave heights, in centimetres. Students who arrive at any hour ask how long they would wait for a bigger wave if they paddled out right then.
The school posts the answer for every hour each morning.
Your task
Write hoursUntilBigger(heights). heights[i] is the forecast wave height in hour i. Return a list wait of the same length, where wait[i] is the number of hours from hour i until the first later hour with a strictly taller wave. If no later hour has a taller wave, wait[i] is 0.
An empty forecast gives an empty list.
hoursUntilBigger(heights: int[]) → int[]
Examples
heights = [5, 3, 4, 8, 6, 7]
[3, 1, 1, 0, 1, 0]
Hour 0 (5 cm) waits 3 hours for hour 3 (8 cm). Hour 1 (3 cm) waits only 1 hour for the 4 cm wave, and hour 2 (4 cm) waits 1 hour for the 8 cm wave. No later wave beats the 8 cm wave at hour 3, so its answer is 0. Hour 4 (6 cm) waits 1 hour for the 7 cm wave, and the last hour has no later hours, so its answer is 0.
heights = [4, 4, 2, 4]
[0, 0, 1, 0]
A 4 cm wave is not bigger than another 4 cm wave. Only hour 2 (2 cm) sees a taller wave, 1 hour later.
Constraints
- 0 ≤ heights.length ≤ 100,000
- 1 ≤ heights[i] ≤ 1,000
Start with brute force
For each hour, scan forward until a taller wave is found, which takes two nested loops.
On a mostly falling forecast each scan runs to the end: which is about n² / 2 steps, or around 5 billion for 100,000 hours. Every earlier hour rescans the same small waves.
Hints
0 of 4 shown- Hint 1
In
[5, 3, 4, 8], when you reach the 8, which earlier hours get their answer at that moment? - Hint 2
A tall wave answers every earlier, smaller hour still waiting. So the waiting hours, oldest to newest, never get taller: a newer, taller one would already have answered the older one.
- Hint 3
Keep waiting hours on a stack, newest on top. A new wave checks only the top: if taller, it answers it and checks the next; if not, it stops, since everything below is at least as tall.
- Hint 4
Walk the hours left to right with a stack of indexes. For each hour
i: while the stack is not empty andheights[top] < heights[i], poptopand setwait[top] = i - top. Then pushi. Anything left on the stack at the end keeps 0.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Turn the question around: let each new wave look back and answer every earlier hour it beats. The waiting hours sit on a stack whose heights never increase from bottom to top, so a new wave only checks the top.
Trace [5, 3, 4, 8, 6, 7] (the stack shows indexes, with heights in brackets):
- Hour 0 (5): nothing is waiting, so push it. The stack is 0(5).
- Hour 1 (3): 3 is not taller than 5, so push it. The stack is 0(5), 1(3).
- Hour 2 (4): 4 > 3, so pop 1 and set wait[1] = 2 - 1 = 1. Then 4 is not taller than 5, so stop and push 2. The stack is 0(5), 2(4).
- Hour 3 (8): pop 2 and set wait[2] = 1, then pop 0 and set wait[0] = 3. Push 3. The stack is 3(8).
- Hour 4 (6): 6 is not taller than 8, so push it. The stack is 3(8), 4(6).
- Hour 5 (7): pop 4 and set wait[4] = 1. Then 7 is not taller than 8, so push 5. The stack is 3(8), 5(7).
Hours 3 and 5 are never answered, so they keep 0, and the result is [3, 1, 1, 0, 1, 0]. The comparison must be < and not <=, because an equal wave is not bigger and must not answer the hour on top of the stack.
function hoursUntilBigger(heights) {
const wait = new Array(heights.length).fill(0);
const waiting = []; // indexes still waiting; their heights never increase upward
for (let i = 0; i < heights.length; i++) {
// the new wave answers every smaller wave still waiting
while (waiting.length > 0 && heights[waiting[waiting.length - 1]] < heights[i]) {
const j = waiting.pop();
wait[j] = i - j;
}
waiting.push(i);
}
return wait; // hours left on the stack keep 0
}def hours_until_bigger(heights):
wait = [0] * len(heights)
waiting = [] # indexes still waiting; their heights never increase upward
for i, h in enumerate(heights):
# the new wave answers every smaller wave still waiting
while waiting and heights[waiting[-1]] < h:
j = waiting.pop()
wait[j] = i - j
waiting.append(i)
return wait # hours left on the stack keep 0Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.