1. 程式人生 > >leetcode496 - Next Greater Element I - easy && leetcode503 - Next Greater Element II - medium

leetcode496 - Next Greater Element I - easy && leetcode503 - Next Greater Element II - medium

處理 The 返回 this spa 模擬 nat numbers hashmap

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;
    }
}

leetcode496 - Next Greater Element I - easy && leetcode503 - Next Greater Element II - medium