Find the Number of Subarrays Where Boundary Elements Are Maximum Problem
Description
LeetCode Problem 3113.
You are given an array of positive integers nums.
Return the number of subarrays of nums, where the first and the last elements of the subarray are equal to the largest element in the subarray.
Example 1:
Input: nums = [1,4,3,3,2]
Output: 6
Explanation:
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
- subarray [1,4,3,3,2], with its largest element 1. The first element is 1 and the last element is also 1.
- subarray [1,4,3,3,2], with its largest element 4. The first element is 4 and the last element is also 4.
- subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [1,4,3,3,2], with its largest element 2. The first element is 2 and the last element is also 2.
- subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
Hence, we return 6.
Example 2:
Input: nums = [3,3,3]
Output: 6
Explanation:
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
- subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
Hence, we return 6.
Example 3:
Input: nums = [1]
Output: 1
Explanation:
There is a single subarray of nums which is [1], with its largest element 1. The first element is 1 and the last element is also 1.
Hence, we return 1.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Monotonic Stack
A subarray counts when its first and last elements are equal and no element exceeds them. Every single element counts by itself.
Keep a stack of pairs [value, count] that is strictly decreasing in value from bottom to top; count is the number of subarrays ending at the current position whose boundary value (and maximum) equals value. For each x, pop every pair with a smaller value. If the stack is then empty or its top is greater than x, push [x, 1] (only the one-element subarray). If the top equals x, increment its count (it extends every earlier such subarray, plus the new single element). Add the top's count to the answer.
Complexity: O(n) time, O(n) space.
class Solution:
def numberOfSubarrays(self, nums: List[int]) -> int:
stk = []
ans = 0
for x in nums:
while stk and stk[-1][0] < x:
stk.pop()
if not stk or stk[-1][0] > x:
stk.append([x, 1])
else:
stk[-1][1] += 1
ans += stk[-1][1]
return ans