LeetCode 739 Daily Temperatures - Implementation

The approach

Monotonic decreasing stack of indices. Pop when warmer day found.

function dailyTemperatures(temperatures): n = len(temperatures) answer = [0] * n stack = [] for i in range(n): while stack and temperatures[i] > temperatures[stack[-1]]: prevDay = stack.pop() answer[prevDay] = i - prevDay stack.push(i) return answer

O(n)O(n) time, O(n)O(n) space.