Monotonic stack - next bigger element

496. The next larger element

https://leetcode.cn/problems/next-greater-element-i/submissions/

Define an array result of the same size as nums1 to store the result

If a position of the result array is not assigned, it should be -1, so it is initialized to -1

In the process of traversing nums2, we need to judge whether nums2[i] has appeared in nums1

Without repeating elements, we can use map for mapping

Find the subscript quickly according to the value, and you can also judge whether nums2[i] has appeared in nums1

Next, we will analyze the following three situations, which must be analyzed clearly.

  1. Case 1:

The current traversed element T[i] is smaller than the top element T[temp.peek()]

At this time, the incremental stack (the order from the stack head to the bottom of the stack) is satisfied, so it is directly pushed into the stack.

  1. Case two:

The current traversed element T[i] is equal to the top element T[temp.peek()]

If they are equal, they are still directly pushed onto the stack, because we are asking for the first element on the right that is larger than ourselves, not greater than or equal to!

  1. Case three:

The current traversed element T[i] is greater than the top element T[temp.peek()]

At this time, if it is pushed into the stack, it will not satisfy the incremental stack. This is also the time to find the first element on the right that is larger than itself.

Determine whether the top element of the stack has appeared in nums1 (note that the elements in the stack are elements of nums2), and if so, start recording the result.

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> temp = new Stack<>();//单调栈,递增
        int[] res = new int[nums1.length];//记录结果集
        Arrays.fill(res,-1);//初始化-1
        HashMap<Integer,Integer> hashmap = new HashMap<>();//利用没有重复的特性
        for(int i=0;i<nums1.length;i++){
                hashmap.put(nums1[i],i);
        }
        temp.add(0);
        for(int i=1;i<nums2.length;i++){
            if(nums2[i] <= nums2[temp.peek()]){
                temp.add(i);
            }else{
                while(!temp.isEmpty() && nums2[temp.peek()] < nums2[i]){
                    if(hashmap.containsKey(nums2[temp.peek()])){
                         Integer index = hashmap.get(nums2[temp.peek()]);
                         res[index] = nums2[i];
                    }
                    temp.pop();
                }
                temp.add(i);
            }

        }
        return res;
    }
}

503. The next bigger element 2

https://leetcode.cn/problems/next-greater-element-ii/submissions/

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int size = nums.length;
        Stack<Integer> temp = new Stack<>();
        int[] res = new int[size];
        if(nums == null || nums.length <=1){
            return new int[]{-1};
        }
        Arrays.fill(res,-1);

        for(int i=0;i<2*size;i++){
            while(!temp.isEmpty() && nums[i%size] > nums[temp.peek()]){
                res[temp.peek()] = nums[i% size];
                    temp.pop();
            }
                temp.push(i%size);
        }
return res;
    }
}

Guess you like

Origin blog.csdn.net/weixin_56194193/article/details/129089892