Advertisement

Next Greater Element I - LeetCode 496 Solution

Next Greater Element I - Complete Solution Guide

Next Greater Element I is LeetCode problem 496, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the 'next greater element' for each number in `nums1` within `nums2`, where `nums1` is a subset of `nums2`. The next greater element for a number `x` is the first element to the right of `x` in `nums2` that is strictly greater than `x`. If no such element exists, we return -1. For each element in `nums1`, we need to find its corresponding position in `nums2`, then search for its next greater element to the right. The inputs are two integer arrays, `nums1` and `nums2`, where `nums1` is a subset of `nums2`, and all elements are unique. The output is an array of the same length as `nums1`, containing the next greater element for each corresponding element in `nums1` within `nums2`.

Solution Approach

The optimal solution utilizes a monotonic stack and a hash map. The monotonic stack helps efficiently determine the next greater element for each number in `nums2`. We iterate through `nums2`. While the stack is not empty and the current element in `nums2` is greater than the top of the stack, we pop the top element and store the current element as its next greater element in the hash map. After processing `nums2`, we iterate through `nums1` and look up the next greater element for each number in the hash map. If a number is not found in the hash map, its next greater element is -1.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack and a hash map to store the next greater element for each number.
  2. Step 2: Iterate through `nums2`.
  3. Step 3: While the stack is not empty and the current element in `nums2` is greater than the element at the top of the stack:
  4. Step 4: Pop the element from the stack and store the current element of `nums2` as its next greater element in the hash map.
  5. Step 5: Push the current element of `nums2` onto the stack.
  6. Step 6: After iterating through `nums2`, initialize an array to store the results for `nums1`.
  7. Step 7: Iterate through `nums1`.
  8. Step 8: For each element in `nums1`, look up its next greater element in the hash map. If it exists, add it to the result array; otherwise, add -1.
  9. Step 9: Return the result array.

Key Insights

  • Insight 1: The key observation is that we need to efficiently find the next greater element for each number in `nums2` to avoid redundant searches. We can precompute these and then use them to answer queries for `nums1` quickly.
  • Insight 2: A stack can be used to keep track of potential next greater elements. The stack maintains a decreasing order, and when we encounter a larger element, we can pop elements from the stack and map them to the larger element as their next greater element.
  • Insight 3: Using a hash map (or unordered_map in C++) to store the mapping between elements and their next greater elements allows for quick lookups.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m)

Topics

This problem involves: Array, Hash Table, Stack, Monotonic Stack.

Companies

Asked at: Accenture, Agoda, Flipkart, Goldman Sachs, Morgan Stanley, Swiggy, Tencent.