Ezvor

2832. Maximal Range That Each Element Is Maximum in It

MediumArray Preparing judge… LeetCode
ArrayStackMonotonic Stack

Hints

Hint 1
For each index, we must find the nearest bigger element on both its left and right sides.
Hint 2
First, find the nearest bigger element on the left side of each element. To do that, use a stack of pairs (value, index).
Hint 3
Start iterating from the beginning of the array.
Hint 4
Whenever we reach an element nums[index], while the top of the stack is smaller than nums[index], we pop from the stack.
Hint 5
If there is an element left in the stack, top.index + 1 would be the answer. Otherwise, 0 is the answer.
Hint 6
After that, we push (nums[index], index) to the stack and go for the next element.