Skip to content

Commit ed3dea8

Browse files
committed
feat: add solutions to lc problem: No.2447
No.2447.Number of Subarrays With GCD Equal to K
1 parent 191e68c commit ed3dea8

File tree

7 files changed

+85
-89
lines changed

7 files changed

+85
-89
lines changed

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747

4848
**方法一:直接枚举**
4949

50-
对于每个子数组,我们可以枚举其左右端点,计算出其最大公因数,然后判断是否等于 $k$ 即可
50+
我们可以枚举 $nums[i]$ 作为子数组的左端点,然后枚举 $nums[j]$ 作为子数组的右端点,其中 $i\le j$。在枚举右端点的过程中,我们可以用一个变量 $g$ 来维护当前子数组的最大公因数,每次枚举到一个新的右端点时,我们更新最大公因数 $g = \gcd(g, nums[j])$。如果 $g=k$,那么当前子数组的最大公因数等于 $k$,我们就将答案增加 $1$
5151

52-
时间复杂度 $O(n^2\times log M)$,其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
52+
枚举结束后,返回答案即可。
53+
54+
时间复杂度 $O(n \times (n + \log M))$,其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
5355

5456
<!-- tabs:start -->
5557

@@ -60,14 +62,12 @@
6062
```python
6163
class Solution:
6264
def subarrayGCD(self, nums: List[int], k: int) -> int:
63-
n = len(nums)
6465
ans = 0
65-
for i in range(n):
66-
x = nums[i]
67-
for j in range(i, n):
68-
x = gcd(x, nums[j])
69-
if x == k:
70-
ans += 1
66+
for i in range(len(nums)):
67+
g = 0
68+
for x in nums[i:]:
69+
g = gcd(g, x)
70+
ans += g == k
7171
return ans
7272
```
7373

@@ -81,10 +81,10 @@ class Solution {
8181
int n = nums.length;
8282
int ans = 0;
8383
for (int i = 0; i < n; ++i) {
84-
int x = nums[i];
84+
int g = 0;
8585
for (int j = i; j < n; ++j) {
86-
x = gcd(x, nums[j]);
87-
if (x == k) {
86+
g = gcd(g, nums[j]);
87+
if (g == k) {
8888
++ans;
8989
}
9090
}
@@ -107,10 +107,10 @@ public:
107107
int n = nums.size();
108108
int ans = 0;
109109
for (int i = 0; i < n; ++i) {
110-
int x = nums[i];
110+
int g = 0;
111111
for (int j = i; j < n; ++j) {
112-
x = __gcd(x, nums[j]);
113-
ans += x == k;
112+
g = gcd(g, nums[j]);
113+
ans += g == k;
114114
}
115115
}
116116
return ans;
@@ -121,17 +121,17 @@ public:
121121
### **Go**
122122
123123
```go
124-
func subarrayGCD(nums []int, k int) int {
125-
ans, n := 0, len(nums)
126-
for i, x := range nums {
127-
for j := i; j < n; j++ {
128-
x = gcd(x, nums[j])
129-
if x == k {
124+
func subarrayGCD(nums []int, k int) (ans int) {
125+
for i := range nums {
126+
g := 0
127+
for _, x := range nums[i:] {
128+
g = gcd(g, x)
129+
if g == k {
130130
ans++
131131
}
132132
}
133133
}
134-
return ans
134+
return
135135
}
136136
137137
func gcd(a, b int) int {
@@ -146,22 +146,22 @@ func gcd(a, b int) int {
146146

147147
```ts
148148
function subarrayGCD(nums: number[], k: number): number {
149-
const n = nums.length;
150149
let ans = 0;
151-
for (let i = 0; i < n; i++) {
152-
let x = nums[i];
153-
for (let j = i; j < n; j++) {
154-
x = gcd(nums[j], x);
155-
if (x == k) ans += 1;
150+
const n = nums.length;
151+
for (let i = 0; i < n; ++i) {
152+
let g = 0;
153+
for (let j = i; j < n; ++j) {
154+
g = gcd(g, nums[j]);
155+
if (g === k) {
156+
++ans;
157+
}
156158
}
157159
}
158160
return ans;
159161
}
160162

161163
function gcd(a: number, b: number): number {
162-
if (a > b) [a, b] = [b, a];
163-
if (a == 0) return b;
164-
return gcd(b % a, a);
164+
return b === 0 ? a : gcd(b, a % b);
165165
}
166166
```
167167

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/README_EN.md

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@
4848
```python
4949
class Solution:
5050
def subarrayGCD(self, nums: List[int], k: int) -> int:
51-
n = len(nums)
5251
ans = 0
53-
for i in range(n):
54-
x = nums[i]
55-
for j in range(i, n):
56-
x = gcd(x, nums[j])
57-
if x == k:
58-
ans += 1
52+
for i in range(len(nums)):
53+
g = 0
54+
for x in nums[i:]:
55+
g = gcd(g, x)
56+
ans += g == k
5957
return ans
6058
```
6159

@@ -67,10 +65,10 @@ class Solution {
6765
int n = nums.length;
6866
int ans = 0;
6967
for (int i = 0; i < n; ++i) {
70-
int x = nums[i];
68+
int g = 0;
7169
for (int j = i; j < n; ++j) {
72-
x = gcd(x, nums[j]);
73-
if (x == k) {
70+
g = gcd(g, nums[j]);
71+
if (g == k) {
7472
++ans;
7573
}
7674
}
@@ -93,10 +91,10 @@ public:
9391
int n = nums.size();
9492
int ans = 0;
9593
for (int i = 0; i < n; ++i) {
96-
int x = nums[i];
94+
int g = 0;
9795
for (int j = i; j < n; ++j) {
98-
x = __gcd(x, nums[j]);
99-
ans += x == k;
96+
g = gcd(g, nums[j]);
97+
ans += g == k;
10098
}
10199
}
102100
return ans;
@@ -107,17 +105,17 @@ public:
107105
### **Go**
108106
109107
```go
110-
func subarrayGCD(nums []int, k int) int {
111-
ans, n := 0, len(nums)
112-
for i, x := range nums {
113-
for j := i; j < n; j++ {
114-
x = gcd(x, nums[j])
115-
if x == k {
108+
func subarrayGCD(nums []int, k int) (ans int) {
109+
for i := range nums {
110+
g := 0
111+
for _, x := range nums[i:] {
112+
g = gcd(g, x)
113+
if g == k {
116114
ans++
117115
}
118116
}
119117
}
120-
return ans
118+
return
121119
}
122120
123121
func gcd(a, b int) int {
@@ -132,22 +130,22 @@ func gcd(a, b int) int {
132130

133131
```ts
134132
function subarrayGCD(nums: number[], k: number): number {
135-
const n = nums.length;
136133
let ans = 0;
137-
for (let i = 0; i < n; i++) {
138-
let x = nums[i];
139-
for (let j = i; j < n; j++) {
140-
x = gcd(nums[j], x);
141-
if (x == k) ans += 1;
134+
const n = nums.length;
135+
for (let i = 0; i < n; ++i) {
136+
let g = 0;
137+
for (let j = i; j < n; ++j) {
138+
g = gcd(g, nums[j]);
139+
if (g === k) {
140+
++ans;
141+
}
142142
}
143143
}
144144
return ans;
145145
}
146146

147147
function gcd(a: number, b: number): number {
148-
if (a > b) [a, b] = [b, a];
149-
if (a == 0) return b;
150-
return gcd(b % a, a);
148+
return b === 0 ? a : gcd(b, a % b);
151149
}
152150
```
153151

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/Solution.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class Solution {
44
int n = nums.size();
55
int ans = 0;
66
for (int i = 0; i < n; ++i) {
7-
int x = nums[i];
7+
int g = 0;
88
for (int j = i; j < n; ++j) {
9-
x = __gcd(x, nums[j]);
10-
ans += x == k;
9+
g = gcd(g, nums[j]);
10+
ans += g == k;
1111
}
1212
}
1313
return ans;

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/Solution.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
func subarrayGCD(nums []int, k int) int {
2-
ans, n := 0, len(nums)
3-
for i, x := range nums {
4-
for j := i; j < n; j++ {
5-
x = gcd(x, nums[j])
6-
if x == k {
1+
func subarrayGCD(nums []int, k int) (ans int) {
2+
for i := range nums {
3+
g := 0
4+
for _, x := range nums[i:] {
5+
g = gcd(g, x)
6+
if g == k {
77
ans++
88
}
99
}
1010
}
11-
return ans
11+
return
1212
}
1313

1414
func gcd(a, b int) int {

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/Solution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ public int subarrayGCD(int[] nums, int k) {
33
int n = nums.length;
44
int ans = 0;
55
for (int i = 0; i < n; ++i) {
6-
int x = nums[i];
6+
int g = 0;
77
for (int j = i; j < n; ++j) {
8-
x = gcd(x, nums[j]);
9-
if (x == k) {
8+
g = gcd(g, nums[j]);
9+
if (g == k) {
1010
++ans;
1111
}
1212
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution:
22
def subarrayGCD(self, nums: List[int], k: int) -> int:
3-
n = len(nums)
43
ans = 0
5-
for i in range(n):
6-
x = nums[i]
7-
for j in range(i, n):
8-
x = gcd(x, nums[j])
9-
if x == k:
10-
ans += 1
4+
for i in range(len(nums)):
5+
g = 0
6+
for x in nums[i:]:
7+
g = gcd(g, x)
8+
ans += g == k
119
return ans
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
function subarrayGCD(nums: number[], k: number): number {
2-
const n = nums.length;
32
let ans = 0;
4-
for (let i = 0; i < n; i++) {
5-
let x = nums[i];
6-
for (let j = i; j < n; j++) {
7-
x = gcd(nums[j], x);
8-
if (x == k) ans += 1;
3+
const n = nums.length;
4+
for (let i = 0; i < n; ++i) {
5+
let g = 0;
6+
for (let j = i; j < n; ++j) {
7+
g = gcd(g, nums[j]);
8+
if (g === k) {
9+
++ans;
10+
}
911
}
1012
}
1113
return ans;
1214
}
1315

1416
function gcd(a: number, b: number): number {
15-
if (a > b) [a, b] = [b, a];
16-
if (a == 0) return b;
17-
return gcd(b % a, a);
17+
return b === 0 ? a : gcd(b, a % b);
1818
}

0 commit comments

Comments
 (0)