Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d13b1e

Browse files
committed
feat: add solutions to lc problem: No.2436
No.2436.Minimum Split Into Subarrays With GCD Greater Than One
1 parent 297edde commit 0d13b1e

File tree

7 files changed

+116
-60
lines changed

7 files changed

+116
-60
lines changed

‎solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:贪心 + 数学**
64+
65+
对于数组中的每个元素,如果它与前面的元素的最大公约数为 1ドル,ドル那么它需要作为一个新的子数组的第一个元素。否则,它可以与前面的元素放在同一个子数组中。
66+
67+
因此,我们先初始化一个变量 $g,ドル表示当前子数组的最大公约数。初始时 $g=0,ドル答案变量 $ans=1$。
68+
69+
接下来,我们从前往后遍历数组,维护当前子数组的最大公约数 $g$。如果当前元素 $x$ 与 $g$ 的最大公约数为 1ドル,ドル那么我们需要将当前元素作为一个新的子数组的第一个元素,因此,答案加 1ドル,ドル并将 $g$ 更新为 $x$。否则,当前元素可以与前面的元素放在同一个子数组中。继续遍历数组,直到遍历结束。
70+
71+
时间复杂度 $O(n \times \log m),ドル空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别是数组的长度和数组中元素的最大值。
72+
6373
<!-- tabs:start -->
6474

6575
### **Python3**
@@ -69,12 +79,12 @@
6979
```python
7080
class Solution:
7181
def minimumSplits(self, nums: List[int]) -> int:
72-
ans, x = 1, nums[0]
73-
for v in nums:
74-
x = gcd(x, v)
75-
if x == 1:
76-
x = v
82+
ans, g = 1, 0
83+
for x in nums:
84+
g = gcd(g, x)
85+
if g == 1:
7786
ans += 1
87+
g = x
7888
return ans
7989
```
8090

@@ -85,12 +95,12 @@ class Solution:
8595
```java
8696
class Solution {
8797
public int minimumSplits(int[] nums) {
88-
int ans = 1, x = nums[0];
89-
for (int v : nums) {
90-
x = gcd(x, v);
91-
if (x == 1) {
92-
x = v;
98+
int ans = 1, g = 0;
99+
for (int x : nums) {
100+
g = gcd(g, x);
101+
if (g == 1) {
93102
++ans;
103+
g = x;
94104
}
95105
}
96106
return ans;
@@ -108,12 +118,12 @@ class Solution {
108118
class Solution {
109119
public:
110120
int minimumSplits(vector<int>& nums) {
111-
int ans = 1, x = nums[0];
112-
for (int v : nums) {
113-
x = gcd(x, v);
114-
if (x == 1) {
115-
x = v;
121+
int ans = 1, g = 0;
122+
for (int x : nums) {
123+
g = gcd(g, x);
124+
if (g == 1) {
116125
++ans;
126+
g = x;
117127
}
118128
}
119129
return ans;
@@ -125,12 +135,12 @@ public:
125135
126136
```go
127137
func minimumSplits(nums []int) int {
128-
ans, x := 1, nums[0]
129-
for _, v := range nums {
130-
x = gcd(x, v)
131-
if x == 1 {
132-
x = v
138+
ans, g := 1, 0
139+
for _, x := range nums {
140+
g = gcd(g, x)
141+
if g == 1 {
133142
ans++
143+
g = x
134144
}
135145
}
136146
return ans
@@ -147,7 +157,22 @@ func gcd(a, b int) int {
147157
### **TypeScript**
148158

149159
```ts
160+
function minimumSplits(nums: number[]): number {
161+
let ans = 1;
162+
let g = 0;
163+
for (const x of nums) {
164+
g = gcd(g, x);
165+
if (g == 1) {
166+
++ans;
167+
g = x;
168+
}
169+
}
170+
return ans;
171+
}
150172

173+
function gcd(a: number, b: number): number {
174+
return b ? gcd(b, a % b) : a;
175+
}
151176
```
152177

153178
### **...**

‎solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ It can be shown that splitting the array into one subarray will make the GCD = 1
5959
```python
6060
class Solution:
6161
def minimumSplits(self, nums: List[int]) -> int:
62-
ans, x = 1, nums[0]
63-
for v in nums:
64-
x = gcd(x, v)
65-
if x == 1:
66-
x = v
62+
ans, g = 1, 0
63+
for x in nums:
64+
g = gcd(g, x)
65+
if g == 1:
6766
ans += 1
67+
g = x
6868
return ans
6969
```
7070

@@ -73,12 +73,12 @@ class Solution:
7373
```java
7474
class Solution {
7575
public int minimumSplits(int[] nums) {
76-
int ans = 1, x = nums[0];
77-
for (int v : nums) {
78-
x = gcd(x, v);
79-
if (x == 1) {
80-
x = v;
76+
int ans = 1, g = 0;
77+
for (int x : nums) {
78+
g = gcd(g, x);
79+
if (g == 1) {
8180
++ans;
81+
g = x;
8282
}
8383
}
8484
return ans;
@@ -96,12 +96,12 @@ class Solution {
9696
class Solution {
9797
public:
9898
int minimumSplits(vector<int>& nums) {
99-
int ans = 1, x = nums[0];
100-
for (int v : nums) {
101-
x = gcd(x, v);
102-
if (x == 1) {
103-
x = v;
99+
int ans = 1, g = 0;
100+
for (int x : nums) {
101+
g = gcd(g, x);
102+
if (g == 1) {
104103
++ans;
104+
g = x;
105105
}
106106
}
107107
return ans;
@@ -113,12 +113,12 @@ public:
113113
114114
```go
115115
func minimumSplits(nums []int) int {
116-
ans, x := 1, nums[0]
117-
for _, v := range nums {
118-
x = gcd(x, v)
119-
if x == 1 {
120-
x = v
116+
ans, g := 1, 0
117+
for _, x := range nums {
118+
g = gcd(g, x)
119+
if g == 1 {
121120
ans++
121+
g = x
122122
}
123123
}
124124
return ans
@@ -135,7 +135,22 @@ func gcd(a, b int) int {
135135
### **TypeScript**
136136

137137
```ts
138+
function minimumSplits(nums: number[]): number {
139+
let ans = 1;
140+
let g = 0;
141+
for (const x of nums) {
142+
g = gcd(g, x);
143+
if (g == 1) {
144+
++ans;
145+
g = x;
146+
}
147+
}
148+
return ans;
149+
}
138150

151+
function gcd(a: number, b: number): number {
152+
return b ? gcd(b, a % b) : a;
153+
}
139154
```
140155

141156
### **...**

‎solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int minimumSplits(vector<int>& nums) {
4-
int ans = 1, x = nums[0];
5-
for (int v : nums) {
6-
x = gcd(x, v);
7-
if (x == 1) {
8-
x = v;
4+
int ans = 1, g = 0;
5+
for (int x : nums) {
6+
g = gcd(g, x);
7+
if (g == 1) {
98
++ans;
9+
g = x;
1010
}
1111
}
1212
return ans;

‎solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
func minimumSplits(nums []int) int {
2-
ans, x := 1, nums[0]
3-
for _, v := range nums {
4-
x = gcd(x, v)
5-
if x == 1 {
6-
x = v
2+
ans, g := 1, 0
3+
for _, x := range nums {
4+
g = gcd(g, x)
5+
if g == 1 {
76
ans++
7+
g = x
88
}
99
}
1010
return ans

‎solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int minimumSplits(int[] nums) {
3-
int ans = 1, x = nums[0];
4-
for (int v : nums) {
5-
x = gcd(x, v);
6-
if (x == 1) {
7-
x = v;
3+
int ans = 1, g = 0;
4+
for (int x : nums) {
5+
g = gcd(g, x);
6+
if (g == 1) {
87
++ans;
8+
g = x;
99
}
1010
}
1111
return ans;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def minimumSplits(self, nums: List[int]) -> int:
3-
ans, x = 1, nums[0]
4-
for v in nums:
5-
x = gcd(x, v)
6-
if x == 1:
7-
x = v
3+
ans, g = 1, 0
4+
for x in nums:
5+
g = gcd(g, x)
6+
if g == 1:
87
ans += 1
8+
g = x
99
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function minimumSplits(nums: number[]): number {
2+
let ans = 1;
3+
let g = 0;
4+
for (const x of nums) {
5+
g = gcd(g, x);
6+
if (g == 1) {
7+
++ans;
8+
g = x;
9+
}
10+
}
11+
return ans;
12+
}
13+
14+
function gcd(a: number, b: number): number {
15+
return b ? gcd(b, a % b) : a;
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /