My Java Solution Using Two Stacks


  • 0
    S
    public int[] dailyTemperatures(int[] temperatures) {
        int[] index = new int[temperatures.length];
    
        Stack<Integer> ind_stk = new Stack<>();
        Stack<Integer> num_stk = new Stack<>();
    
        for (int i = 0; i < temperatures.length; i++) {
            while (!num_stk.isEmpty() && temperatures[i] > num_stk.peek()) {
                num_stk.pop();
                int ind = ind_stk.pop();
                index[ind] = i - ind;
            }
    
            num_stk.push(temperatures[i]);
            ind_stk.push(i);
        }
    
        return index;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.