Talentd
FleetCode

2832. Maximal Range That Each Element Is Maximum in It

Medium75.5% acceptance1 companyPremium elsewhere, free here

You are given a 0-indexed array nums of distinct integers.

Let us define a 0-indexed array ans of the same length as nums in the following way:

  • ans[i] is the maximum length of a subarray nums[l..r], such that the maximum element in that subarray is equal to nums[i].

Return the array ans.

Note that a subarray is a contiguous part of the array.

Example 1:

Input: nums = [1,5,4,3,6]
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 5 is the maximum is nums[0..3] so ans[1] = 4.
For nums[2] the longest subarray in which 4 is the maximum is nums[2..3] so ans[2] = 2.
For nums[3] the longest subarray in which 3 is the maximum is nums[3..3] so ans[3] = 1.
For nums[4] the longest subarray in which 6 is the maximum is nums[0..4] so ans[4] = 5.

Example 2:

Input: nums = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: For nums[i] the longest subarray in which it's the maximum is nums[0..i] so ans[i] = i + 1.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105
  • All elements in nums are distinct.

Asked at 1 company

Hints open one at a time

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.
12:51
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1,5,4,3,6]
Input
[1,2,3,4,5]
Your code is saved on this device.Solution