mediumStackPattern: Monotonic Stack
Next Greater Element Indices Solution
Problem Statement
You are given a list of integers `thickness` representing the thickness of tomes. Find the next greater element index for each element, which is the index of the first element to its right that is greater than it. If no greater element exists, the result should be -1.
Examples
Example 1:
Input:[1, 2, 3, 4, 5]
Output:[2, 3, 4, 5, -1]
Explanation: The next thicker tome for each tome is the next thicker one in the list, if exists, otherwise -1
Example 2:
Input:[5, 4, 3, 2, 1]
Output:[-1, -1, -1, -1, -1]
Explanation: No thicker tome exists for any tome in the list
Constraints
- The input temperature array will not be empty.
- There will be at most 10,000 elements in the temperature array.
Time: O(n) Space: O(n)
Use a monotonic stack to find the next thicker tome in O(n) time complexity
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
