[LeetCode] 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.
Constraints:
1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 104
- All integers in
nums1
andnums2
are unique. - All the integers of
nums1
also appear innums2
.
Follow up: Could you find an O(nums1.length + nums2.length)
solution?
下一个更大元素 I。
nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中 nums1 是 nums2 的子集。
对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。
返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/next-greater-element-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给定两个没有重复元素的数组 nums1 和 nums2 ,其中 nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出 -1。
思路是单调栈。创建一个 hashmap,记录 num2 中每个元素和他们各自的下一个更大元素。再遍历 num1,看 num1 里面的元素是否在 hashmap 中有对应的 value,有就输出,没有就返回 -1。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public int[] nextGreaterElement(int[] nums1, int[] nums2) { 3 HashMap<Integer, Integer> map = new HashMap<>(); 4 Deque<Integer> stack = new ArrayDeque<>(); 5 for (int num : nums2) { 6 while (!stack.isEmpty() && stack.peek() < num) { 7 map.put(stack.pop(), num); 8 } 9 stack.push(num); 10 } 11 12 int[] res = new int[nums1.length]; 13 for (int i = 0; i < nums1.length; i++) { 14 res[i] = map.getOrDefault(nums1[i], -1); 15 } 16 return res; 17 } 18 }
JavaScript实现
1 /** 2 * @param {number[]} nums1 3 * @param {number[]} nums2 4 * @return {number[]} 5 */ 6 var nextGreaterElement = function (nums1, nums2) { 7 let map = new Map(); 8 let stack = []; 9 for (let num of nums2) { 10 while (stack.length > 0 && stack[stack.length - 1] < num) { 11 map.set(stack.pop(), num); 12 } 13 stack.push(num); 14 } 15 let res = new Array(nums1.length).fill(-1); 16 for (let i = 0; i < res.length; i++) { 17 res[i] = map.get(nums1[i]) || -1; 18 } 19 return res; 20 };