Skip to main content

Next Greater Element I

LeetCode 496 | Difficulty: Easy​

Easy

Problem Description​

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] <= 10^4`

- All integers in `nums1` and `nums2` are **unique**.

- All the integers of `nums1` also appear in `nums2`.

Follow up: Could you find an O(nums1.length + nums2.length) solution?

Topics: Array, Hash Table, Stack, Monotonic Stack


Approach​

Hash Map​

Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?

When to use

Need fast lookups, counting frequencies, finding complements/pairs.

Stack​

Use a stack (LIFO) to track elements that need future processing. Process elements when a "trigger" condition is met (e.g., finding a smaller/larger element). Monotonic stack maintains elements in sorted order for next greater/smaller element problems.

When to use

Matching brackets, next greater element, evaluating expressions, backtracking history.


Solutions​

Solution 1: C# (Best: 211 ms)​

MetricValue
Runtime211 ms
Memory40.1 MB
Date2022-01-18
Solution
public class Solution {
public int[] NextGreaterElement(int[] nums1, int[] nums2) {
Stack<int> s = new Stack<int>();
Dictionary<int, int> dict = new Dictionary<int, int>();
foreach (var item in nums2)
{
while(s.Count != 0 && s.Peek() < item)
{
dict.Add(s.Pop(), item);
}

s.Push(item);
}
int[] result = new int[nums1.Length];
for (int i = 0; i < nums1.Length; i++)
{
result[i] = dict.ContainsKey(nums1[i]) ? dict[nums1[i]] : -1;
}

return result;
}
}

Complexity Analysis​

ApproachTimeSpace
Hash Map$O(n)$$O(n)$
Stack$O(n)$$O(n)$

Interview Tips​

Key Points
  • Start by clarifying edge cases: empty input, single element, all duplicates.
  • Hash map gives O(1) lookup β€” think about what to use as key vs value.
  • Think about what triggers a pop: is it finding a match, or finding a smaller/larger element?