-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0713.Subarray_Product_Less_Than_K.py
More file actions
42 lines (30 loc) · 1.01 KB
/
0713.Subarray_Product_Less_Than_K.py
File metadata and controls
42 lines (30 loc) · 1.01 KB
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
"""
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
"""
#求 count, 最大窗口,最大问题, 类型二
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
res = 0
i = 0
prod = 1
for j in range(len(nums)):
prod *= nums[j]
while i < j and prod >= k:
prod /= nums[i]
i += 1
if prod < k:
res += j - i + 1
return res