713. Subarray Product Less Than K

Overview
Count down the number of kinds of subarrays with a product less than k.
Solution 1: Brute Force
Thinking
Using a nested loop to check all kinds of subarrays.
It will result in a Time Limit Exceeded error.
Algorithm
-
Initialize the
counter
to zero. -
Iterate from 0 to the length of the
nums
array.- Reset the
product
tonums[i]
. - Iterate from
i
to the length of thenums
array.- Product with
nums[j]
, without including the case wherei == j
because it would result in redundant products. - If the
product
is greater or equal tok
, indicating it doesn't satisfy the target, break the loop. - Otherwise, increase the
counter
by 1.
- Product with
- Reset the
-
Return the
counter
as the answer.
Implement
class Solution: def numSubarrayProductLessThanK(self, nums: list[int], k: int) -> int: counter = 0 for i in range(len(nums)): product = nums[i] for j in range(i, len(nums)): if i != j: product *= nums[j] if product >= k: break counter += 1 return counter
Complexity Analysis
Time complexity: O(n^2)
The nested loop takes O(n^2).
Space complexity: O(1)
Solution 2: Slide Window
Thinking
The numbers in the array are all positive.
The product will only increase and won't throw ZeroDivisionError when dividing the number.
Algorithm
-
Initialize
left
to zero to point on the head of the array.
Initializecounter
to zero.
Initializeproduct
to one. We can't set it to zero because a zero product with any number still stays at zero. -
Iterate from 0 to the length of the
nums
array.-
Product with the number of the
right
index in thenums
array. -
While
left
is smaller thanright
andproduct
is greater thank
,- we should divide with the number of the
left
index in thenums
array - and move the
left
pointer.
- we should divide with the number of the
-
Counter increase with
right - left + 1
that include all the subarray counts inside this slide window.
-
-
Return the
counter
as the answer.
Implement
class Solution: def numSubarrayProductLessThanK(self, nums: list[int], k: int) -> int: left = 0 counter = 0 product = 1 for right in range(len(nums)): product *= nums[right] while left <= right and product >= k: product //= nums[left] left += 1 counter += (right - left + 1) return counter
Complexity Analysis
Time complexity: O(n)
The algorithm iterates through the input array nums
using a single for loop.
Inside the loop, there are nested operations for shrinking the window,
but since left is incremented a total number of n times during the whole array traversal,
each element in the array is visited at most twice.
The nested loop terminates when the product becomes less than k,
and this can only happen at most n times total (once for each element).
Therefore, the overall time complexity is 2n, which we describe as O(n).
Space complexity: O(1)