[Monotone Stack] LeetCode-496. The next bigger element I

496. The Next Greater Element I

Title description

Give you two arrays nums1 and nums2 with no repeated elements, where nums1 is a subset of nums2.

Please find out the next greater value of each element in nums1 in nums2.

The next greater element of the number x in nums1 refers to the first element greater than x to the right of the corresponding position in nums2. If it does not exist, the corresponding position outputs -1.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For the number 4 in num1, you cannot enter the second The next higher number is found in the array, so -1 is output.
For the number 1 in num1, the next larger number to the right of the number 1 in the second array is 3.
For the number 2 in num1, there is no next higher number in the second array, so -1 is output.
Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For the number 2 in num1, the next larger number in the second array It is 3.
For the number 4 in num1, there is no next higher number in the second array, so -1 is output.

Problem-solving ideas

Violence law:

For each element in nums1[i], first find it in nums2, and then traverse to the right to find the first element greater than nums1[i].

  /**
     * 暴力法
     * 关键信息:两个数组各自 没有重复元素。
     * 模拟题目的意思:对于每一个 nums1[i] 中的元素,先在 nums2 中找到它,然后向右遍历找到第 1 个大于 nums1[i] 的元素。
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
    
    

        //把数组2的元素放进map中,键为num2的值,值为下标
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
    
    
            map.put(nums2[i],i);
        }
        //定义需要返回的数组
        int[] res = new int[nums1.length];
        //依次遍历num1中的元素,
        for (int i = 0; i < nums1.length; i++) {
    
    
            for (int j = map.get(nums1[i]); j < nums2.length; j++) {
    
    
                if (nums2[j] > nums1[i]){
    
    
                    res[i] = nums2[j];
                    break;
                }
                else {
    
    
                    res[i] = -1;
                }
            }
        }
        return res;
    }

Monotonic stack

  /**
     * 单调栈
     * @param nums1
     * @param nums2
     * @return
     */
    public static int[] nextGreaterElement_1(int[] nums1, int[] nums2) {
    
    
        int len1 = nums1.length;
        int len2 = nums2.length;

        Deque<Integer> stack = new ArrayDeque<>();
        Map<Integer,Integer> map = new HashMap<>();
        //先处理nums2,把对应关系存入哈希表
        for (int i = 0; i < len2; i++) {
    
    
            while (!stack.isEmpty() && stack.peekLast() < nums2[i]){
    
    
                map.put(stack.removeLast(),nums2[i]);
            }
            stack.addLast(nums2[i]);
        }
        int[] res = new int[len1];
        for (int i = 0; i < len1; i++) {
    
    
            res[i] = map.getOrDefault(nums1[i],-1);
        }
        return res;
    }

Guess you like

Origin blog.csdn.net/qq_35655602/article/details/115216490