The number of subarrays where max(subarray) <= target is easier to calculate.
The answer is count(right) - count(left - 1).
count(bound) counts subarrays where no element exceeds bound.
- Define
count_under(bound):- Use a sliding window or a simple counter to find contiguous segments where elements are
<= bound. - For a segment of length
L, there areL*(L+1)//2subarrays.
- Use a sliding window or a simple counter to find contiguous segments where elements are
- Return
count_under(right) - count_under(left - 1).
- Time Complexity: O(N).
- Space Complexity: O(1).
def num_subarray_bounded_max(nums, left, right):
def count(bound):
res = 0
curr = 0
for x in nums:
if x <= bound:
curr += 1
res += curr
else:
curr = 0
return res
return count(right) - count(left - 1)