Compute days until a higher reading
Problem Description
You are provided with an array, readings, which represents sensor values recorded over consecutive days. For each day i, your task is to determine the number of days one must wait until a strictly higher reading is observed on a future day j (where j > i). If no such future day exists with a strictly higher reading, the output for day i should be 0.
Input
- An integer array
readingsof lengthn.
Output
- An integer array
ansof lengthn, whereans[i]denotes the number of days until a strictly higher reading occurs, or0if no such reading exists.
Example
- Input:
readings = [30, 38, 30, 36, 35, 40, 28] - Output:
[1, 4, 1, 2, 1, 0, 0]
Constraints
1 <= n <= 2e5- Readings fit within 32-bit signed integers.
Note
- "Higher" specifically means strictly greater (
>), not greater-than-or-equal-to.
AI interview prep
Core dilemma
Efficiently finding the 'next strictly greater element' for each position in an array, optimizing for time complexity beyond a naive O(N^2) approach, while managing space.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute days until a higher reading
Problem Description
You are provided with an array, readings, which represents sensor values recorded over consecutive days. For each day i, your task is to determine the number of days one must wait until a strictly higher reading is observed on a future day j (where j > i). If no such future day exists with a strictly higher reading, the output for day i should be 0.
Input
- An integer array
readingsof lengthn.
Output
- An integer array
ansof lengthn, whereans[i]denotes the number of days until a strictly higher reading occurs, or0if no such reading exists.
Example
- Input:
readings = [30, 38, 30, 36, 35, 40, 28] - Output:
[1, 4, 1, 2, 1, 0, 0]
Constraints
1 <= n <= 2e5- Readings fit within 32-bit signed integers.
Note
- "Higher" specifically means strictly greater (
>), not greater-than-or-equal-to.