1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public int numSubarrayProductLessThanK(int[] nums, int k) { if (k <= 1) { return 0; }
int windowProduct = 1; int subArrayCount = 0;
int i = 0; for (int j = 0; j < nums.length; j++) { windowProduct *= nums[j];
while (windowProduct >= k) { windowProduct /= nums[i++]; } subArrayCount += j - i + 1; }
return subArrayCount; } }
|
References
713. Subarray Product Less Than K
剑指 Offer II 009. 乘积小于 K 的子数组