text function dailyTemperatures(temperatures): n = length(temperatures) answer = array of n zeros stack = empty stack // stores indices for i from 0 to n-1: while not stack.isEmpty() and temperatures[i] > temperatures[stack.top()]: prevIndex = stack.pop() answer[prevIndex] = i - prevIndex stack.push(i) return answer Time complexity: . Each index is pushed and popped at most once. Space complexity: . Stack can hold all indices in the worst case.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/stacks/daily-temperatures-pseudocode
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████████████████████████████████████████████████████████████