---
# System prepended metadata

title: 0795. Number of Subarrays with Bounded Maximum
tags: [Leetcode, Medium]

---

# 0795. Number of Subarrays with Bounded Maximum
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/
## 思路 $O(N)$ $O(1)$
count(B) is the number of subarrays that have all elements less than or equal to B.
## Code
```java=
class Solution {
    public int numSubarrayBoundedMax(int[] nums, int left, int right) {
        return count(nums, right)-count(nums, left-1);
    }
    private int count(int[] nums, int bound){
        int ans = 0, curLen = 0;
        for(int num:nums){
            if(num<=bound) curLen++;
            else curLen = 0;
            ans += curLen;
        }
        return ans;
    }
}
```