795.区间子数组个数
java
class Solution {
public int numSubarrayBoundedMax(int[] nums, int left, int right) {
return lessEqualsThan(nums,right) - lessEqualsThan(nums,left - 1);
}
private int lessEqualsThan(int[] nums,int k){
int len = nums.length;
int res = 0,left = 0,right = 0;
//循环不变量:nums[left,right]里的所有元素都小于等于k
while( right < len){
if(nums[right] > k){
left = right + 1;
}
res += right - left;
right++;
}
return res;
}
}