Wait Time Until a Strictly Larger Sensor Value
Stacks & QueuesAuto-graded · 21 test casesLast updated
Find, for each position in a sequence, the distance to the next strictly larger element, using a monotonic stack to achieve linear time.
A sensor logs one integer value per day, and the values are stored in a list values, where values[i] is the reading on day i.
For every day i, determine how many days later the sensor first reports a value that is strictly larger than values[i]. In other words, find the smallest j > i with values[j] > values[i] and report j - i. If no such day exists, report 0 for that day.
Return a list of the same length as values, where entry i holds the answer for day i.
Rules
- A later reading that is merely equal to the current one does not count; it must be strictly greater.
- Only days after day
iare considered. - The last day always has the answer
0.
Constraints
1 <= len(values) <= 2 * 10^5- Each value fits in a 32-bit signed integer (it may be negative or zero).
- The input should be solved efficiently enough for the maximum length; a quadratic scan is too slow.
Examples
Example 1
Input: wait_for_larger_value([30, 38, 30, 36, 35, 40, 28])
Output: [1, 4, 1, 2, 1, 0, 0]
Hints
Hint 1
A brute-force scan to the right from each day is O(n^2) and fails on long non-increasing inputs. Look for a way to share work between days.
Hint 2
Sweep left to right and remember the days that are still waiting for a larger reading. When a new reading arrives, it may resolve several of those waiting days at once.
Hint 3
Keep a stack of indices (not values) whose readings are non-increasing from bottom to top. Pop while the top reading is strictly smaller than the current one, and record the index difference. Use strict comparison so equal values do not resolve each other.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is the classic "next greater element" problem, solved with a monotonic stack.
Scan the days from left to right while maintaining a stack of indices whose answers are still unknown (days that are waiting for a larger reading). When processing day i with value v:
- While the stack is not empty and the reading at the index on top is strictly less than
v, pop that indexpand setresult[p] = i - p. Dayiis the first larger reading forp. - Push
ionto the stack.
Indices that remain on the stack at the end never encountered a larger reading, so they keep their default answer of 0.
Why it is correct
The readings at the stack's indices are non-increasing from bottom to top: a new index is pushed only after every strictly smaller reading above it has been popped. Suppose index p is popped at day i. Then values[i] > values[p]. If some earlier day k with p < k < i also had values[k] > values[p], then at day k the stack above p would have held only values no larger than values[p], all smaller than values[k], so they would be popped, and then p itself would be popped at k. That contradicts it being popped at i. Hence i is the first strictly larger day. Indices left on the stack would have been popped by any later larger value, so 0 is right for them.
Using a strict < when popping means equal readings never resolve each other, matching the "strictly larger" rule.
Complexity
- Time: O(n). Each index is pushed once and popped at most once.
- Space: O(n) for the result and, in the worst case, the stack.
Reference solution
from typing import List
def wait_for_larger_value(values: List[int]) -> List[int]:
result = [0] * len(values)
waiting = [] # indices still waiting for a strictly larger value
for i, current in enumerate(values):
while waiting and values[waiting[-1]] < current:
prev = waiting.pop()
result[prev] = i - prev
waiting.append(i)
return result