MrainW's Home

All things come to those who wait!

0%

LeetCode 496. Next Greater Element I

Question

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int i = nums2.length -1; i >= 0; i--){
while (!stack.isEmpty() && nums2[i] >= stack.peek()) stack.pop();
map.put(nums2[i], stack.isEmpty() ? -1 : 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;
}
}

Time complexity: O(n)

Space complexity: O(n)

Welcome to my other publishing channels