Skip to content

Commit 0a2e473

Browse files
committed
feat: add solutions to lc problem: No.0713
No.0713.Subarray Product Less Than K
1 parent 7b9daf7 commit 0a2e473

File tree

10 files changed

+197
-106
lines changed

10 files changed

+197
-106
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- [区间加法](/solution/0300-0399/0370.Range%20Addition/README.md) - 前缀和、差分
4747
- [用邮票贴满网格图](/solution/2100-2199/2132.Stamping%20the%20Grid/README.md) - 二维前缀和、二维差分
4848
- [无重复字符的最长子串](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README.md) - 双指针、哈希表
49+
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - 双指针
4950
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - 位运算、lowbit
5051
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - 区间合并
5152

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
4545
- [Range Addition](/solution/0300-0399/0370.Range%20Addition/README_EN.md) - Prefix sum, Difference array
4646
- [ Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - Prefix sum, Difference array
4747
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md) - Two pointers, Hash table
48+
- [Subarray Product Less Than K](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README_EN.md) - Two pointers
4849
- [Number of 1 Bits](/solution/0100-0199/0191.Number%20of%201%20Bits/README_EN.md) - Bit manipulation, Lowbit
4950
- [Merge Intervals](/solution/0000-0099/0056.Merge%20Intervals/README_EN.md) - Merge intervals
5051

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@
5858

5959
时间复杂度 O(n),其中 n 表示字符串 s 的长度。
6060

61+
双指针算法模板:
62+
63+
```java
64+
for (int i = 0, j = 0; i < n; ++i) {
65+
while (j < i && check(j, i)) {
66+
++j;
67+
}
68+
// 具体问题的逻辑
69+
}
70+
```
71+
6172
<!-- tabs:start -->
6273

6374
### **Python3**

solution/0700-0799/0713.Subarray Product Less Than K/README.md

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,107 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
<!-- tabs:start -->
43-
44-
### **C++**
45-
46-
```cpp
47-
class Solution {
48-
public:
49-
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
50-
int left = 0, right;
51-
long mul = 1;
52-
int count = 0;
53-
54-
for (right = 0; right < nums.size(); right++) {
55-
mul *= nums[right];
56-
57-
while(left <= right && mul >= k) {
58-
mul /= nums[left++];
59-
}
42+
**方法一:双指针**
6043

61-
count += right >= left? right - left + 1: 0;
62-
}
44+
双指针算法模板:
6345

64-
return count;
46+
```java
47+
for (int i = 0, j = 0; i < n; ++i) {
48+
while (j < i && check(j, i)) {
49+
++j;
6550
}
66-
};
51+
// 具体问题的逻辑
52+
}
6753
```
6854

55+
时间复杂度 O(n)。
56+
57+
<!-- tabs:start -->
58+
6959
### **Python3**
7060

7161
<!-- 这里可写当前语言的特殊实现逻辑 -->
7262

7363
```python
74-
64+
class Solution:
65+
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
66+
ans, s, j = 0, 1, 0
67+
for i, v in enumerate(nums):
68+
s *= v
69+
while j <= i and s >= k:
70+
s //= nums[j]
71+
j += 1
72+
ans += i - j + 1
73+
return ans
7574
```
7675

7776
### **Java**
7877

7978
<!-- 这里可写当前语言的特殊实现逻辑 -->
8079

8180
```java
81+
class Solution {
82+
public int numSubarrayProductLessThanK(int[] nums, int k) {
83+
int ans = 0;
84+
for (int i = 0, j = 0, s = 1; i < nums.length; ++i) {
85+
s *= nums[i];
86+
while (j <= i && s >= k) {
87+
s /= nums[j++];
88+
}
89+
ans += i - j + 1;
90+
}
91+
return ans;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
102+
int ans = 0;
103+
for (int i = 0, j = 0, s = 1; i < nums.size(); ++i)
104+
{
105+
s *= nums[i];
106+
while (j <= i && s >= k) s /= nums[j++];
107+
ans += i - j + 1;
108+
}
109+
return ans;
110+
}
111+
};
112+
```
82113
114+
### **Go**
115+
116+
```go
117+
func numSubarrayProductLessThanK(nums []int, k int) int {
118+
ans := 0
119+
for i, j, s := 0, 0, 1; i < len(nums); i++ {
120+
s *= nums[i]
121+
for ; j <= i && s >= k; j++ {
122+
s /= nums[j]
123+
}
124+
ans += i - j + 1
125+
}
126+
return ans
127+
}
83128
```
84129

85130
### **TypeScript**
86131

87132
```ts
88133
function numSubarrayProductLessThanK(nums: number[], k: number): number {
89-
if (k <= 1) {
90-
return 0;
91-
}
92-
93-
const n = nums.length;
94-
let res = 0;
95-
for (let i = 0; i < n; i++) {
96-
let product = 1;
97-
for (let j = i; j < n; j++) {
98-
product *= nums[j];
99-
if (product >= k) {
100-
break;
101-
}
102-
res++;
134+
let ans = 0;
135+
for (let i = 0, j = 0, s = 1; i < nums.length; ++i) {
136+
s *= nums[i];
137+
while (j <= i && s >= k) {
138+
s /= nums[j++];
103139
}
140+
ans += i - j + 1;
104141
}
105-
return res;
142+
return ans;
106143
}
107144
```
108145

solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,64 +37,86 @@ Note that [10, 5, 2] is not included as the product of 100 is not strictly less
3737

3838
<!-- tabs:start -->
3939

40-
### **C++**
40+
### **Python3**
4141

42-
```cpp
43-
class Solution {
44-
public:
45-
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
46-
int left = 0, right;
47-
long mul = 1;
48-
int count = 0;
42+
```python
43+
class Solution:
44+
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
45+
ans, s, j = 0, 1, 0
46+
for i, v in enumerate(nums):
47+
s *= v
48+
while j <= i and s >= k:
49+
s //= nums[j]
50+
j += 1
51+
ans += i - j + 1
52+
return ans
53+
```
4954

50-
for (right = 0; right < nums.size(); right++) {
51-
mul *= nums[right];
55+
### **Java**
5256

53-
while(left <= right && mul >= k) {
54-
mul /= nums[left++];
57+
```java
58+
class Solution {
59+
public int numSubarrayProductLessThanK(int[] nums, int k) {
60+
int ans = 0;
61+
for (int i = 0, j = 0, s = 1; i < nums.length; ++i) {
62+
s *= nums[i];
63+
while (j <= i && s >= k) {
64+
s /= nums[j++];
5565
}
56-
57-
count += right >= left? right - left + 1: 0;
66+
ans += i - j + 1;
5867
}
59-
60-
return count;
68+
return ans;
6169
}
62-
};
70+
}
6371
```
6472

65-
### **Python3**
66-
67-
```python
73+
### **C++**
6874

75+
```cpp
76+
class Solution {
77+
public:
78+
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
79+
int ans = 0;
80+
for (int i = 0, j = 0, s = 1; i < nums.size(); ++i)
81+
{
82+
s *= nums[i];
83+
while (j <= i && s >= k) s /= nums[j++];
84+
ans += i - j + 1;
85+
}
86+
return ans;
87+
}
88+
};
6989
```
7090
71-
### **Java**
72-
73-
```java
74-
91+
### **Go**
92+
93+
```go
94+
func numSubarrayProductLessThanK(nums []int, k int) int {
95+
ans := 0
96+
for i, j, s := 0, 0, 1; i < len(nums); i++ {
97+
s *= nums[i]
98+
for ; j <= i && s >= k; j++ {
99+
s /= nums[j]
100+
}
101+
ans += i - j + 1
102+
}
103+
return ans
104+
}
75105
```
76106

77107
### **TypeScript**
78108

79109
```ts
80110
function numSubarrayProductLessThanK(nums: number[], k: number): number {
81-
if (k <= 1) {
82-
return 0;
83-
}
84-
85-
const n = nums.length;
86-
let res = 0;
87-
for (let i = 0; i < n; i++) {
88-
let product = 1;
89-
for (let j = i; j < n; j++) {
90-
product *= nums[j];
91-
if (product >= k) {
92-
break;
93-
}
94-
res++;
111+
let ans = 0;
112+
for (let i = 0, j = 0, s = 1; i < nums.length; ++i) {
113+
s *= nums[i];
114+
while (j <= i && s >= k) {
115+
s /= nums[j++];
95116
}
117+
ans += i - j + 1;
96118
}
97-
return res;
119+
return ans;
98120
}
99121
```
100122

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
class Solution {
22
public:
33
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
4-
int left = 0, right;
5-
long mul = 1;
6-
int count = 0;
7-
8-
for (right = 0; right < nums.size(); right++) {
9-
mul *= nums[right];
10-
11-
while(left <= right && mul >= k) {
12-
mul /= nums[left++];
13-
}
14-
15-
count += right >= left? right - left + 1: 0;
4+
int ans = 0;
5+
for (int i = 0, j = 0, s = 1; i < nums.size(); ++i)
6+
{
7+
s *= nums[i];
8+
while (j <= i && s >= k) s /= nums[j++];
9+
ans += i - j + 1;
1610
}
17-
18-
return count;
11+
return ans;
1912
}
2013
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func numSubarrayProductLessThanK(nums []int, k int) int {
2+
ans := 0
3+
for i, j, s := 0, 0, 1; i < len(nums); i++ {
4+
s *= nums[i]
5+
for ; j <= i && s >= k; j++ {
6+
s /= nums[j]
7+
}
8+
ans += i - j + 1
9+
}
10+
return ans
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int numSubarrayProductLessThanK(int[] nums, int k) {
3+
int ans = 0;
4+
for (int i = 0, j = 0, s = 1; i < nums.length; ++i) {
5+
s *= nums[i];
6+
while (j <= i && s >= k) {
7+
s /= nums[j++];
8+
}
9+
ans += i - j + 1;
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
3+
ans, s, j = 0, 1, 0
4+
for i, v in enumerate(nums):
5+
s *= v
6+
while j <= i and s >= k:
7+
s //= nums[j]
8+
j += 1
9+
ans += i - j + 1
10+
return ans

0 commit comments

Comments
 (0)