website/content/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum.md
We are given an array nums of positive integers, and two positive integers left and right (left <= right).
Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least left and at most right.
Example: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].
Note:
left, right, and nums[i] will be an integer in the range [0, 109].nums will be in the range of [1, 50000].给定一个元素都是正整数的数组A ,正整数 L 以及 R (L <= R)。求连续、非空且其中最大元素满足大于等于L 小于等于R的子数组个数。
package leetcode
func numSubarrayBoundedMax(nums []int, left int, right int) int {
return getAnswerPerBound(nums, right) - getAnswerPerBound(nums, left-1)
}
func getAnswerPerBound(nums []int, bound int) int {
res, count := 0, 0
for _, num := range nums {
if num <= bound {
count++
} else {
count = 0
}
res += count
}
return res
}