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.
Given an array of integers nums, return an array containing the indices of the next greater element for each number in nums.
If a number doesn't have a next greater element, denote that with -1.
Example 1:
Input: nums = [1, 3, 2, 4] Output: [1, 3, 3, -1] Explanation: The next greater element for each value of nums is as follows: - next greater element for 1 is 3 which is located at index 1 - next greater element for 3 is 4 which is located at index 3 - next greater element for 2 is 4 which is located at index 3 - 4 is the last and doesn't have a next greater element so we mark it with -1
Example 2:
Input: nums = [7, 3, 2, 6, 11, 9, 8, 10, 13] Output: [4, 3, 3, 4, 8, 7, 7, 8, -1]
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check