mediumStackPattern: Monotonic Stack
Daily Temperatures Solution
Problem Statement
Given an array of integers `temperatures` representing daily temperatures, find the number of days until a warmer temperature occurs for each day. If no warmer temperature occurs, output 0.
Examples
Example 1:
Input:[73,74,75,71,69,72,76,73]
Output:[1,1,4,2,1,1,0,0]
Explanation: For each day, find the number of days until a warmer temperature occurs
Constraints
- 1 <= n <= 10^5
- 30 <= temp[i] <= 100
Time: O(N) Space: O(N)
Iterate from left to right. Maintain a monotonic decreasing stack of indices. If current temp > temp[stack top], pop top, answer[top] = current index - top. Time O(N), Space O(N).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
