The problem. You are given two arrays of distinct numbers, nums1 and nums2, where every number in nums1 also appears in nums2. For each x in nums1, find x in nums2 and return the first number to its right that is greater than `x`. If there isn't one, the answer is -1.
Input: nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
Output: [-1, 3, -1]
Input: nums1 = [2, 4], nums2 = [1, 2, 3, 4]
Output: [3, -1]"The first bigger number to the right" is the next greater element — a monotonic stack question. nums1 only says which answers to report.
For each x in nums1, search nums2 for x, then walk right until something bigger appears.
function nextGreaterElement(nums1, nums2) { return nums1.map((x) => { let j = nums2.indexOf(x); // find x in nums2 for (j = j + 1; j < nums2.length; j++) { if (nums2[j] > x) return nums2[j]; // the first bigger one to its right } return -1; });}Time O(m · n), space O(1) beyond the output, for m = nums1.length and n = nums2.length. Each query may scan nearly all of nums2.
Solve the question once for all of `nums2`, store the answers in a hash map, and then each query is a lookup. To find every next greater element in one pass, walk nums2 from the right, keeping a stack of candidates. A candidate that is ≤ the current number can never be the answer for anything further left — the current number is closer and at least as big — so pop it. Whatever is left on top is the answer.
function nextGreaterElement(nums1, nums2) { const next = new Map(); // value -> its next greater element const stack = []; // candidates, decreasing upward for (let i = nums2.length - 1; i >= 0; i--) { const x = nums2[i]; while (stack.length > 0 && stack[stack.length - 1] <= x) stack.pop(); next.set(x, stack.length > 0 ? stack[stack.length - 1] : -1); stack.push(x); } return nums1.map((x) => next.get(x));}Time O(n + m), space O(n). Each number of nums2 is pushed once and popped at most once, and each query is a hash map lookup.
There's a second way to run the same stack that many find more natural — and it's the template you'll reuse in the next lessons. Walk nums2 left to right. Each number joins a waiting list (the stack) until a bigger number shows up. When a number x arrives, it is the answer for every waiting number smaller than it — pop them all and record x for each. Because smaller numbers keep getting popped, the waiting stack is always decreasing upward.
Watch each arrival answer the smaller numbers that were waiting.
Time O(n + m), space O(n). Same cost as the optimal solution, but answers are written straight into ans for the numbers that were asked about — no map of answers for all of nums2. More importantly, "pop everything the new arrival beats" is the exact move behind Next Greater Element II, Largest Rectangle in Histogram and Trapping Rain Water.
O(m · n)Each of the m queries may scan nearly all n numbers.
O(1)Nothing beyond the output.
O(n + m)Each number is pushed once and popped once; each query is one map lookup.
O(n)The stack and a map of answers for all of nums2.
O(n + m)The same pushes and pops, writing answers straight into ans.
O(n)The stack and an index map for nums1.