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
counterto zero. -
Iterate from 0 to the length of the
numsarray.- Reset the
producttonums[i]. - Iterate from
ito the length of thenumsarray.- Product with
nums[j], without including the case wherei == jbecause it would result in redundant products. - If the
productis greater or equal tok, indicating it doesn't satisfy the target, break the loop. - Otherwise, increase the
counterby 1.
- Product with
- Reset the
-
Return the
counteras 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
leftto zero to point on the head of the array.
Initializecounterto zero.
Initializeproductto 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
numsarray.-
Product with the number of the
rightindex in thenumsarray. -
While
leftis smaller thanrightandproductis greater thank,- we should divide with the number of the
leftindex in thenumsarray - and move the
leftpointer.
- we should divide with the number of the
-
Counter increase with
right - left + 1that include all the subarray counts inside this slide window.
-
-
Return the
counteras 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)