You're given an array of distinct integers, and your task is to find the maximum range where each element can be the dominant leader!
For each element nums[i] in the array, you need to determine the longest possible subarray where nums[i] is the maximum element. Think of it as finding the "sphere of influence" for each number - how far can its dominance extend?
Goal: Return an array ans where ans[i] represents the maximum length of any subarray that contains nums[i] as its maximum element.
Example: In array [2, 1, 4, 9, 3], the element 9 can dominate the entire array (length 5), while 1 can only dominate a subarray of length 1 (just itself).
Input & Output
Visualization
Time & Space Complexity
Two passes through the array, each element pushed and popped at most once
Space for the monotonic stack and result arrays
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 106
- All elements in nums are distinct
- The array is 0-indexed