Next Greater Element I

easy array hash map stack monotonic stack

Problem

The next greater element of a value x in nums2 is the first value to its right that is strictly greater than x; or −1 if no such value exists. Given nums1 (a subset of nums2 with unique values), return the next greater element of each nums1[i] in nums2.

Inputnums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
Output[-1, 3, -1]
4 has no greater on its right; 1's next greater is 3; 2 has none.

def next_greater_element(nums1, nums2):
    nxt = {}
    stack = []
    for x in nums2:
        while stack and stack[-1] < x:
            nxt[stack.pop()] = x
        stack.append(x)
    return [nxt.get(v, -1) for v in nums1]
function nextGreaterElement(nums1, nums2) {
  const nxt = new Map();
  const stack = [];
  for (const x of nums2) {
    while (stack.length && stack[stack.length - 1] < x) {
      nxt.set(stack.pop(), x);
    }
    stack.push(x);
  }
  return nums1.map(v => nxt.has(v) ? nxt.get(v) : -1);
}
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Map<Integer, Integer> nxt = new HashMap<>();
        Deque<Integer> stack = new ArrayDeque<>();
        for (int x : nums2) {
            while (!stack.isEmpty() && stack.peek() < x) {
                nxt.put(stack.pop(), x);
            }
            stack.push(x);
        }
        int[] ans = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) ans[i] = nxt.getOrDefault(nums1[i], -1);
        return ans;
    }
}
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
    unordered_map<int,int> nxt;
    vector<int> stack;
    for (int x : nums2) {
        while (!stack.empty() && stack.back() < x) {
            nxt[stack.back()] = x; stack.pop_back();
        }
        stack.push_back(x);
    }
    vector<int> ans(nums1.size());
    for (int i = 0; i < (int)nums1.size(); i++) ans[i] = nxt.count(nums1[i]) ? nxt[nums1[i]] : -1;
    return ans;
}
Time: O(n1 + n2) Space: O(n2)