If the product of elements from l to r is < k, then all subarrays ending at r starting between l and r also satisfy the condition. The number of such subarrays is r - l + 1.
- If
k <= 1, return 0. l = 0,prod = 1,res = 0.- For each
r,prod *= nums[r]. - While
prod >= k(andl <= r):prod /= nums[l].l += 1.
res += (r - l + 1).- Return
res.
- Time Complexity: O(N).
- Space Complexity: O(1).
def num_subarray_product_less_than_k(nums, k):
if k <= 1: return 0
res = 0
prod = 1
l = 0
for r in range(len(nums)):
prod *= nums[r]
while prod >= k:
prod /= nums[l]
l += 1
res += (r - l + 1)
return res