Skip to content

Commit c7a813f

Browse files
committed
Resolved 'Subarray Product Less Than K' LeetCode problem with an optimized solution
1 parent a607db5 commit c7a813f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Subarray Product Less Than K
2+
3+
class Solution(object):
4+
def numSubarrayProductLessThanK(self, nums, k):
5+
if k <= 1:
6+
return 0
7+
8+
left, right, product, count = 0, 0, 1, 0
9+
n = len(nums)
10+
11+
while right < n:
12+
product *= nums[right]
13+
while product >= k:
14+
product //= nums[left]
15+
left += 1
16+
count += 1 + (right - left)
17+
right += 1
18+
19+
return count
20+
21+
solution = Solution()
22+
print(solution.numSubarrayProductLessThanK([10,5,2,6], 100))

0 commit comments

Comments
 (0)