Find the Number of Subarrays Where Boundary Elements Are Maximum
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
6There are 6 qualifying subarrays: each single-element subarray and the subarray [3, 3].
Example 2
Input
nums = [3,3,3]Output
6All 6 subarrays have first and last elements equal to the largest element, since every element is 3.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9