SprintCode.pro

Подготовка к алгоритмическим задачам

Super

Daily Temperatures

Description: Given daily temperatures, for each day return how many days until a warmer temperature. If there is none, put 0.

Example 1:

Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Example 2:

Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]

Constraints:

1 <= temperatures.length <= 10⁵

30 <= temperatures[i] <= 100

Recommended time and space complexity

Aim for O(n) time and O(n) space.


Hint 1

Brute force over pairs is O(n²). Can you do a single pass?


Hint 2

Keep indices of days whose answer is not known yet in a stack.


Hint 3

When a warmer day arrives, you found the answer for the stack top. Pop while current is warmer.

A monotonic stack problem. Teaches finding the next greater element in one pass instead of quadratic search.

Expected Input :

[73,74,75,71,69,72,76,73]

Expected Output

[1,1,4,2,1,1,0,0]