3382. Find the Number of Subarrays Where Boundary Elements Are Maximum

Hard
Array
Binary Search
Stack
Monotonic Stack

Description

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 <= 105
  • 1 <= nums[i] <= 109

Hints

Hint 1
For each element <code>nums[i]</code>, we can count the number of valid subarrays ending with it.
Hint 2
For each index <code>i</code>, find the nearest index <code>j</code> on its left <code>(j < i)</code> such that <code>nums[j] < nums[i]</code>. This can be done via a monotonic stack.
Hint 3
For each index <code>i</code>, find the number of indices <code>k</code> in the window <code>[j + 1, i]</code> such that <code>nums[k] == nums[i]</code>, this is the number of the valid subarrays ending with <code>nums[i]</code>. This can be done by sliding window.
Hint 4
Sum the answer of all the indices <code>i</code> to get the final result.
Hint 5
Is it possible to use DSU as an alternate solution?

Statistics

Acceptance
32.5%
Submissions
43,533
Accepted
14,162