Subarray Product Less Than K
Given an array of integers nums
and an integer k
, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k
.
Example 1:
Approach: Sliding Window
The idea is always keep an
max-product-window
less thanK
;Every time shift window by adding a new number on the right(
j
), if the product is greater than k, then try to reduce numbers on the left(i
), until the subarray product fit less thank
again, (subarray could be empty);Each step introduces
x
new subarrays, where x is the size of the current window(j + 1 - i)
; example: for window (5, 2), when 6 is introduced, it add 3 new subarray: (5, (2, (6)))
Time: O(n) Space: O(1)
Last updated
Was this helpful?