美文网首页
[刷题防痴呆] 0496 - 下一个更大的元素 I (Next

[刷题防痴呆] 0496 - 下一个更大的元素 I (Next

作者: 西出玉门东望长安 | 来源:发表于2022-01-15 01:25 被阅读0次

    题目地址

    https://leetcode.com/problems/next-greater-element-i/

    题目描述

    496. Next Greater Element I
    
    The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
    
    You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
    
    For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
    
    Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
    
     
    
    Example 1:
    
    Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
    Output: [-1,3,-1]
    Explanation: The next greater element for each value of nums1 is as follows:
    - 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
    - 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
    - 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
    Example 2:
    
    Input: nums1 = [2,4], nums2 = [1,2,3,4]
    Output: [3,-1]
    Explanation: The next greater element for each value of nums1 is as follows:
    - 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
    - 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
    
    

    思路

    • 模拟可解.
    • 单调栈. 反向的话, 用单调栈维护当前位置右边的更大的元素列表,从栈底到栈顶的元素单调递减.
    • 正向的话, 用单调栈维护当前位置还没找到最大值的元素列表, 从栈底到栈顶的元素单调递减.

    关键点

    代码

    • 语言支持:Java
    
    // 暴力 O(mn)
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            int[] res = new int[nums1.length];
    
            for (int i = 0; i < nums1.length; i++) {
                int index = 0;
                for (int j = 0; j < nums2.length; j++) {
                    if (nums1[i] == nums2[j]) {
                        index = j;
                        break;
                    }
                }
                int k = index;
                for (; k < nums2.length; k++) {
                    if (nums2[k] > nums2[index]) {
                        break;
                    }
                }
    
                if (k == nums2.length) {
                    res[i] = -1;
                } else {
                    res[i] = nums2[k];
                }
            }
    
            return res;
        }
    }
    
    // 单调栈 O(m + n) 反向
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            Deque<Integer> stack = new ArrayDeque<>();
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = nums2.length - 1; i >= 0; i--) {
                int num = nums2[i];
                while (!stack.isEmpty() && stack.peek() <= num) {
                    stack.pop();
                }
                if (stack.isEmpty()) {
                    map.put(nums2[i], -1);
                } else {
                    map.put(nums2[i], stack.peek());
                }
                stack.push(nums2[i]);
            }
            int[] res = new int[nums1.length];
            for (int i = 0; i < nums1.length; i++) {
                res[i] = map.get(nums1[i]);
            }
    
            return res;
        }
    }
    
    // 单调栈 正向
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            Deque<Integer> stack = new ArrayDeque<>();
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums2.length; i++) {
                int num = nums2[i];
                while (!stack.isEmpty() && stack.peek() < num) {
                    map.put(stack.pop(), num);
                }
                stack.push(num);
            }
    
            int[] res = new int[nums1.length];
            for (int i = 0; i < nums1.length; i++) {
                res[i] = map.getOrDefault(nums1[i], -1);
            }
    
            return res;
        }
    }
    
    // 单调栈正向, 存index
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            Deque<Integer> stack = new ArrayDeque<>();
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums2.length; i++) {
                while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) {
                    int index = stack.pop();
                    map.put(nums2[index], nums2[i]);
                }
                stack.push(i);
            }
    
            int[] res = new int[nums1.length];
            for (int i = 0; i < nums1.length; i++) {
                res[i] = map.getOrDefault(nums1[i], -1);
            }
            return res;
        }
    }
    

    相关文章

      网友评论

          本文标题:[刷题防痴呆] 0496 - 下一个更大的元素 I (Next

          本文链接:https://www.haomeiwen.com/subject/xfdqcrtx.html