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.

  1. Use a stack to store indices.
  2. For each temperature:
    • While current temp > temp at stack top:
      • Pop index.
      • Answer[index] = currentDay - index.
  3. Print final answer array (space-separated).

01EXAMPLE 1

Input[73, 74, 75, 71, 69, 72, 76, 73]
Output1 1 4 2 1 1 0 0

Explanation: Days to wait.

Constraints

  • O(n) time complexity.
Data StructuresStack
JavaScript
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