Daily Temperatures
HardAcc. 84.6%
+45 XP 20
Future Warmth
Given temperatures, find how many days you wait for a warmer one. Use a monotonic stack to keep track of indices whose temperatures aren't yet solved.
The Assignment
Your function receives temps.
- Use a stack to store indices.
- For each temperature:
- While current temp > temp at stack top:
- Pop index.
- Answer[index] = currentDay - index.
- While current temp > temp at stack top:
- Print final answer array (space-separated).
01EXAMPLE 1
Input
[73, 74, 75, 71, 69, 72, 76, 73]Output
1 1 4 2 1 1 0 0Explanation: Days to wait.
Constraints
- O(n) time complexity.
Data StructuresStack
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
temps[73,74,75,71,69,72,76,73]
Expected Output
1 1 4 2 1 1 0 0
Click RUN to test your solution