题目描述
子集积小于k,求子集数
(暴力枚举) $O(n)$
Python 代码
def numSubarrayProductLessThanK(self, nums, k):
if k <= 1:
return 0
n = len(nums)
count, left, prod = 0, 0, 1
for right, n in enumerate(nums):
prod *= n
while prod >= k:
prod //= nums[left]
left += 1
count += right - left + 1
return count