Number of Subarrays with Bounded Maximum

Given an integer array nums and two integers left and right, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right].

The test cases are generated so that the answer will fit in a 32-bit integer.

Example 1
Inputnums = [2,1,4,3], left = 2, right = 3
Output3
There are three subarrays that meet the requirements: [2], [2, 1], and [3].
Example 2
Inputnums = [2,9,2,5,6], left = 2, right = 8
Output7
There are seven contiguous non-empty subarrays whose maximum element is in the range [2, 8].

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • 0 <= left <= right <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate