713. Subarray Product Less Than K
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 53% Topics: Array, Binary Search, Sliding Window, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def numSubarrayProductLessThanK(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
if k <= 1: return 0
result, start, prod = 0, 0, 1
for i, num in enumerate(nums):
prod *= num
while prod >= k:
prod /= nums[start]
start += 1
result += i-start+1
return result
Solution from kamyu104/LeetCode-Solutions · MIT