Skip to content
Decoder

Next Greater Element I

Easy

Problem

You get two lists of distinct integers, nums1 and nums2, where every value in nums1 also appears somewhere in nums2.

For each value x in nums1, find where x sits in nums2 and look to its right: the answer is the **first value to the right of x in nums2 that is greater than x**, or -1 if there is none. Return the answers in the order of nums1.

Example 1

Input
nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
Output
[-1, 3, -1]

Example 2

Input
nums1 = [2, 4], nums2 = [1, 2, 3, 4]
Output
[3, -1]

Constraints

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 10^4
  • All values in each list are unique, and every value of nums1 is in nums2.
  • Aim for O(nums1.length + nums2.length).

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.