给你一个整数数组 nums
和一个整数 k
,请你返回子数组内所有元素的乘积严格小于 k
的连续子数组的数目。
var numSubarrayProductLessThanK = function(nums, k) {
var ans = 0;
let start = 0;
let end = 0;
let count = 0;
let cur_m = 1;
while(end < nums.length) {
if(nums[end] < k) {
if(cur_m * nums[end] >= k) {
cur_m /= nums[start];
start++;
count--;
} else {
cur_m = cur_m * nums[end];
count++;
ans += count;
end++;
}
} else {
start = ++end;
cur_m = 1;
count = 0;
}
}
return ans;
};