Skip to content

Files

Latest commit

Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
and
Zanger67/leetcode
Jan 20, 2025
8d4889c · Jan 20, 2025

History

History
75 lines (57 loc) · 2.05 KB

_713. Subarray Product Less Than K.md

File metadata and controls

75 lines (57 loc) · 2.05 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 01, 2024

Last updated : July 01, 2024


Related Topics : Array, Binary Search, Sliding Window, Prefix Sum

Acceptance Rate : 52.21 %


Solutions

Python

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