leetcode496

496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
1. All elements in nums1 and nums2 are unique.
2. The length of both nums1 and nums2 would not exceed 1000.

503. Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
Example 1:
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
Note: The length of given array won't exceed 10000.

Stack。O(n)。和next warm temperature题基本一样。Daily Temperatures的博客: https://www.cnblogs.com/jasminemzy/p/9665976.html

Next Greater Element I就是直接对nums2做一样的处理,区别只是额外维护一个map存所有答案,之后回到num1的时候取出部分答案产生返回结果即可。

Next Greater Element II 的循环数组处理就是假装nums重复了两次。
原因:因为重复两次后,比如1234变成12341234,在double数组里你每个元素的第一个位置开始数四格,等同于single数组里每个元素往后按循环数组规则数四格。那么你double数组里做出来的第一个更大元素正确性就等同于single数组里做出来的了。
模拟重复的方法:用取余%。循环的时候循环次数翻倍 [2 * length - 1, 0],一进来做一个下标转换 int trueI = i % nums.length;

实现1. Next Greater Element I

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        Map<Integer, Integer> map = new HashMap<>();
        
        for (int i = nums2.length - 1; i >= 0; i--) {
            if (stack.isEmpty() || nums2[i] >= stack.peek()) {
                while (!stack.isEmpty() && nums2[i] >= stack.peek()) {
                    stack.pop();
                }
                int val = stack.isEmpty() ? -1 : stack.peek();
                map.put(nums2[i], val);
                stack.push(nums2[i]);
            } else {
                map.put(nums2[i], stack.peek());
                stack.push(nums2[i]);
            }
        }
        
        int[] ans = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            ans[i] = map.get(nums1[i]);
        }
        return ans;
    }
}

实现2:

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int[] ans = new int[nums.length];
        
        for (int idx = 2 * nums.length - 1; idx >= 0; idx--) {
            int i = idx % nums.length;
            if (stack.isEmpty() || nums[i] >= stack.peek()) {
                while (!stack.isEmpty() && nums[i] >= stack.peek()) {
                    stack.pop();
                }
                int val = stack.isEmpty() ? -1 : stack.peek();
                ans[i] = val;
                stack.push(nums[i]);
            } else {
                ans[i] = stack.peek();
                stack.push(nums[i]);
            }
        }
        
        return ans;
    }
}
原文地址:https://www.cnblogs.com/jasminemzy/p/9667146.html