Problem
Submissions

Solution: Next Greater Element I

Statement

Naive approach

The naive approach is to select each element of nums1 and search for its occurrence in nums2. If the element is found, we look for the occurrence of its next greater element in nums2 linearly. If the next greater element is obtained, we store it in the ans array in the corresponding place to the element in nums1. Otherwise, we store −1-1 in the ans array for that element.

The overall time complexity of the algorithm becomes O(n1×n2)O(n_1 \times n_2), because we’re searching for each element of the nums1 array in the nums2 array. The space complexity of this algorithm is O(1)O(1).

Optimized approach using hash map

An optimized approach to solve this problem is using a hash map and a stack. A hash map is used to store the elements in nums2 as keys and their next greater elements as the respective values.

The algorithm proceeds through the following steps after creating an empty stack and a hash map:

  • Iterate over each element of nums2, and if the stack is not empty, compare it with the top element of the stack.

    • If the current element of nums2 is greater than the top element of the stack, pop the top element from the stack and put a key-value pair in the hash map with the popped element as the key and the current element of nums2 as the value.

    • This process uses a monotonic stackA monotonic stack maintains elements in either increasing or decreasing order, allowing efficient retrieval of the next greater or smaller elements in a sequence. that maintains elements in decreasing order, ensuring that the top of the stack always has the smallest element yet to find its next greater element. This allows efficient resolution of next greater elements.

    • Repeat the step above until either the stack becomes empty or the current element of nums2 is not greater than the top element of the stack.

  • After each iteration over nums2, push the current element of nums2 onto the stack.

  • After processing all the elements of nums2, check if any elements are still remaining in the stack. If they are, pop them and put key-value pairs in the hash map with the remaining elements as the keys and −1-1 as their respective values.

  • Finally, create an ans array with the same length as nums1 and populate it with the values from the hash map that correspond to the keys in nums1.

  • Return the ans array containing the next greater element for each element in nums1.

Let’s look at the following illustration to get a better understanding of the solution:

Problem
Submissions

Solution: Next Greater Element I

Statement

Naive approach

The naive approach is to select each element of nums1 and search for its occurrence in nums2. If the element is found, we look for the occurrence of its next greater element in nums2 linearly. If the next greater element is obtained, we store it in the ans array in the corresponding place to the element in nums1. Otherwise, we store −1-1 in the ans array for that element.

The overall time complexity of the algorithm becomes O(n1×n2)O(n_1 \times n_2), because we’re searching for each element of the nums1 array in the nums2 array. The space complexity of this algorithm is O(1)O(1).

Optimized approach using hash map

An optimized approach to solve this problem is using a hash map and a stack. A hash map is used to store the elements in nums2 as keys and their next greater elements as the respective values.

The algorithm proceeds through the following steps after creating an empty stack and a hash map:

  • Iterate over each element of nums2, and if the stack is not empty, compare it with the top element of the stack.

    • If the current element of nums2 is greater than the top element of the stack, pop the top element from the stack and put a key-value pair in the hash map with the popped element as the key and the current element of nums2 as the value.

    • This process uses a monotonic stackA monotonic stack maintains elements in either increasing or decreasing order, allowing efficient retrieval of the next greater or smaller elements in a sequence. that maintains elements in decreasing order, ensuring that the top of the stack always has the smallest element yet to find its next greater element. This allows efficient resolution of next greater elements.

    • Repeat the step above until either the stack becomes empty or the current element of nums2 is not greater than the top element of the stack.

  • After each iteration over nums2, push the current element of nums2 onto the stack.

  • After processing all the elements of nums2, check if any elements are still remaining in the stack. If they are, pop them and put key-value pairs in the hash map with the remaining elements as the keys and −1-1 as their respective values.

  • Finally, create an ans array with the same length as nums1 and populate it with the values from the hash map that correspond to the keys in nums1.

  • Return the ans array containing the next greater element for each element in nums1.

Let’s look at the following illustration to get a better understanding of the solution: