Talentd
FleetCode

795. Number of Subarrays with Bounded Maximum

Medium55.4% acceptance4 companies

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:

Input: nums = [2,1,4,3], left = 2, right = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].

Example 2:

Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 109
  • 0 <= left <= right <= 109

Asked at 4 companies

32:15
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[2,1,4,3]
2
3
Input
[2,9,2,5,6]
2
8
Your code is saved on this device.Solution