968. Maximal Range That Each Element is Maximal in It
An array of unique integers with a 0-index is provided to you.
Here's how we define a 0-indexed array ans that is the same length as nums:
- If the greatest element in a subarray nums[l..r] equals nums[i], then ans[i] is the maximum length of that subarray.
Return the array answer.
A subarray is an adjacent portion of the array, so keep that in mind.
Example 1:
Input : nums = [1, 2, 3, 4, 5]
Output : [1, 2, 3, 4, 5]
Explanation :
For nums[0] the longest subarray in which 1 is the maximum is nums[0..0] so ans[0] = 1.
For nums[1] the longest subarray in which 2 is the maximum is nums[0..1] so ans[1] = 2.
For nums[2] the longest subarray in which 3 is the maximum is nums[0..2] so ans[2] = 3.
For nums[3] the longest subarray in which 4 is the maximum is nums[0..3] so ans[3] = 4.
For nums[4] the longest subarray in which 5 is the maximum is nums[0..4] so ans[4] = 5.
Example 2:
Input : nums = [1, 4, 3, 2, 5]
Output : [1, 4, 2, 1, 5]
Explanation :
For nums[0] the longest subarray in which 1 is the maximum is nums[0..0] so ans[0] = 1.
For nums[1] the longest subarray in which 4 is the maximum is nums[0..3] so ans[1] = 4.
For nums[2] the longest subarray in which 3 is the maximum is nums[2..3] so ans[2] = 2.
For nums[3] the longest subarray in which 2 is the maximum is nums[3..3] so ans[3] = 1.
For nums[4] the longest subarray in which 5 is the maximum is nums[0..4] so ans[4] = 5.
Now Your Turn!
Pick the correct output for the given inputInput : nums = [4, 3, 5, 1, 2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 1 <= nums[i] <= 106
- All elements in nums are unique.