All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 01, 2024
Last updated : July 01, 2024
Related Topics : Array, Binary Search, Sliding Window, Prefix Sum
Acceptance Rate : 52.21 %
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
nums2 = [1] + [0] * len(nums)
for i in range(0, len(nums)) :
nums2[i + 1] = nums[i] * nums2[i]
counter = 0
left, right = 0, 1
while right < len(nums2) :
if (nums2[right] / nums2[left] < k) :
counter += right - left
right += 1
else :
left += 1
if left >= right :
right += 1
return counter
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
current = nums[0]
counter = 0
left, right = 0, 0
while right < len(nums) :
if (current < k) :
counter += right - left + 1
right += 1
if right < len(nums) :
current *= nums[right]
else :
current //= nums[left]
left += 1
if left > right :
right += 1
if right < len(nums) :
current *= nums[right]
return counter