November 23, 2019

739. Daily Temperatures

739. Daily Temperatures

递减栈 Descending Stack, Time = O(n), Space = O(n)

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int[] wait = new int[T.length];
        Deque<Integer> stack = new ArrayDeque<>();
        for (int day = 0; day < T.length; day++) {
            while (! stack.isEmpty() && T[day] > T[stack.peekFirst()]) {
                int previousDay = stack.pollFirst();
                wait[previousDay] = day - previousDay;
            }
            stack.offerFirst(day);
        }
        return wait; 
    }
}
comments powered by Disqus