-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsubarray-product-less-than-k.py
157 lines (131 loc) · 4.58 KB
/
subarray-product-less-than-k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
713. Subarray Product Less Than K
Medium
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:
Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
Example 2:
Input: nums = [1,2,3], k = 0
Output: 0
Constraints:
1 <= nums.length <= 3 * 104
1 <= nums[i] <= 1000
0 <= k <= 106
"""
# V0
# IDEA : SLIDING WINDOW
# MAINTAIN 2 INDEX : left, i, SO THE SLIDING WINDOW IS : [left, i]
# CHECK IF THE PRODUCT OF ALL DIGITS IN THE WINDOW [left, i] < k
# IF NOT, REMOVE CURRENT LEFT, AND DO LEFT ++
# REPEAT ABOVE PROCESS AND GO THOROUGH ALL ARRAY
class Solution:
def numSubarrayProductLessThanK(self, nums, k):
# init values
product = 1
i = 0
result = 0
for j, num in enumerate(nums):
### NOTE : we get product first
product *= num
### NOTE : the while loop condition : product >= k
# -> if product >= k, we do the corresponding op
while i <= j and product >= k:
### NOTE this trick
# -> divided the number back, since this number already make the product > k
product = product // nums[i]
### NOTE : move i to 1 right index
i += 1
### NOTE : , the number of intervals with subarray product less than k and with right-most coordinate right, is right - left + 1
# -> https://leetcode.com/problems/subarray-product-less-than-k/solution/
result += (j - i + 1)
return result
# V1
# https://www.jiuzhang.com/solution/subarray-product-less-than-k/#tag-highlight-lang-python
# IDEA : SLIDING WINDOW
# MAINTAIN 2 INDEX : left, i, SO THE SLIDING WINDOW IS : [left, i]
# CHECK IF THE PRODUCT OF ALL DIGITS IN THE WINDOW [left, i] < k
# IF NOT, REMOVE CURRENT LEFT, AND DO LEFT ++
# REPEAT ABOVE PROCESS AND GO THOROUGH ALL ARRAY
class Solution:
def numSubarrayProductLessThanK(self, nums, k):
product, i, result = 1, 0, 0
for j, num in enumerate(nums):
product *= num
while i <= j and product >= k:
product //= nums[i] # divided the number back, since this number already make the product > k
i += 1
result += j - i + 1
return result
# V1'
# http://bookshadow.com/weblog/2017/10/22/leetcode-subarray-product-less-than-k/
class Solution(object):
def numSubarrayProductLessThanK(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
product = 1
ll = rr = -1
ans = 0
for num in nums:
rr += 1
product *= num
while ll + 1 <= rr and product >= k:
product /= nums[ll + 1]
ll += 1
ans += rr - ll
return ans
# V1'
# IDEA : BINARY SEARCH
# https://leetcode.com/problems/subarray-product-less-than-k/solution/
class Solution(object):
def numSubarrayProductLessThanK(self, nums, k):
if k == 0: return 0
k = math.log(k)
prefix = [0]
for x in nums:
prefix.append(prefix[-1] + math.log(x))
ans = 0
for i, x in enumerate(prefix):
j = bisect.bisect(prefix, x + k - 1e-9, i+1)
ans += j - i - 1
return ans
# V1''
# IDEA : SLIDING WINDOW
# https://leetcode.com/problems/subarray-product-less-than-k/solution/
class Solution(object):
def numSubarrayProductLessThanK(self, nums, k):
if k <= 1: return 0
prod = 1
ans = left = 0
for right, val in enumerate(nums):
prod *= val
while prod >= k:
prod /= nums[left]
left += 1
ans += right - left + 1
return ans
# V2
# 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