Skip to content

Commit 2e1b18f

Browse files
authored
feat: add solutions to lc problem: No.2197 (doocs#2789)
No.2197.Replace Non-Coprime Numbers in Array
1 parent 8621cd5 commit 2e1b18f

File tree

7 files changed

+383
-0
lines changed

7 files changed

+383
-0
lines changed

solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md

+138
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,142 @@
6666

6767
## 解法
6868

69+
### 方法一:栈
70+
71+
如果存在三个相邻的数 $x$, $y$, $z$ 可以进行合并,那么我们先合并 $x$ 和 $y$,再合并 $z$ 的结果,与先合并 $y$ 和 $z$,再合并 $x$ 的结果是一样的,结果均为 $\text{LCM}(x, y, z)$。
72+
73+
因此,我们可以总是优先合并左侧相邻的数,再将合并后的结果与右侧相邻的数进行合并。
74+
75+
我们使用一个栈来模拟这个过程,遍历数组,对于每个数,我们将其入栈,然后不断检查栈顶的两个数是否互质,如果不互质,我们将这两个数出栈,然后将它们的最小公倍数入栈,直到栈顶的两个数互质,或者栈中元素小于两个。
76+
77+
最后栈中的元素即为最终结果。
78+
79+
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $M$ 为数组中的最大值。
80+
81+
<!-- tabs:start -->
82+
83+
```python
84+
class Solution:
85+
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
86+
stk = []
87+
for x in nums:
88+
stk.append(x)
89+
while len(stk) > 1:
90+
x, y = stk[-2:]
91+
g = gcd(x, y)
92+
if g == 1:
93+
break
94+
stk.pop()
95+
stk[-1] = x * y // g
96+
return stk
97+
```
98+
99+
```java
100+
class Solution {
101+
public List<Integer> replaceNonCoprimes(int[] nums) {
102+
List<Integer> stk = new ArrayList<>();
103+
for (int x : nums) {
104+
stk.add(x);
105+
while (stk.size() > 1) {
106+
x = stk.get(stk.size() - 1);
107+
int y = stk.get(stk.size() - 2);
108+
int g = gcd(x, y);
109+
if (g == 1) {
110+
break;
111+
}
112+
stk.remove(stk.size() - 1);
113+
stk.set(stk.size() - 1, (int) ((long) x * y / g));
114+
}
115+
}
116+
return stk;
117+
}
118+
119+
private int gcd(int a, int b) {
120+
if (b == 0) {
121+
return a;
122+
}
123+
return gcd(b, a % b);
124+
}
125+
}
126+
```
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
vector<int> replaceNonCoprimes(vector<int>& nums) {
132+
vector<int> stk;
133+
for (int x : nums) {
134+
stk.push_back(x);
135+
while (stk.size() > 1) {
136+
x = stk.back();
137+
int y = stk[stk.size() - 2];
138+
int g = __gcd(x, y);
139+
if (g == 1) {
140+
break;
141+
}
142+
stk.pop_back();
143+
stk.back() = 1LL * x * y / g;
144+
}
145+
}
146+
return stk;
147+
}
148+
};
149+
```
150+
151+
```go
152+
func replaceNonCoprimes(nums []int) []int {
153+
stk := []int{}
154+
for _, x := range nums {
155+
stk = append(stk, x)
156+
for len(stk) > 1 {
157+
x = stk[len(stk)-1]
158+
y := stk[len(stk)-2]
159+
g := gcd(x, y)
160+
if g == 1 {
161+
break
162+
}
163+
stk = stk[:len(stk)-1]
164+
stk[len(stk)-1] = x * y / g
165+
}
166+
}
167+
return stk
168+
}
169+
170+
func gcd(a, b int) int {
171+
if b == 0 {
172+
return a
173+
}
174+
return gcd(b, a%b)
175+
}
176+
```
177+
178+
```ts
179+
function replaceNonCoprimes(nums: number[]): number[] {
180+
const gcd = (a: number, b: number): number => {
181+
if (b === 0) {
182+
return a;
183+
}
184+
return gcd(b, a % b);
185+
};
186+
const stk: number[] = [];
187+
for (let x of nums) {
188+
stk.push(x);
189+
while (stk.length > 1) {
190+
x = stk.at(-1)!;
191+
const y = stk.at(-2)!;
192+
const g = gcd(x, y);
193+
if (g === 1) {
194+
break;
195+
}
196+
stk.pop();
197+
stk.pop();
198+
stk.push(((x * y) / g) | 0);
199+
}
200+
}
201+
return stk;
202+
}
203+
```
204+
205+
<!-- tabs:end -->
206+
69207
<!-- end -->

solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md

+138
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,142 @@ Note that there are other ways to obtain the same resultant array.
6262

6363
## Solutions
6464

65+
### Solution 1: Stack
66+
67+
If there exist three adjacent numbers $x$, $y$, $z$ that can be merged, then the result of first merging $x$ and $y$, then merging $z$, is the same as the result of first merging $y$ and $z$, then merging $x$. Both results are $\text{LCM}(x, y, z)$.
68+
69+
Therefore, we can always prefer to merge the adjacent numbers on the left, and then merge the result with the adjacent number on the right.
70+
71+
We use a stack to simulate this process. We traverse the array, and for each number, we push it into the stack. Then we continuously check whether the top two numbers of the stack are coprime. If they are not coprime, we pop these two numbers out of the stack, and then push their least common multiple into the stack, until the top two numbers of the stack are coprime, or there are less than two elements in the stack.
72+
73+
The final elements in the stack are the final result.
74+
75+
The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Where $M$ is the maximum value in the array.
76+
77+
<!-- tabs:start -->
78+
79+
```python
80+
class Solution:
81+
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
82+
stk = []
83+
for x in nums:
84+
stk.append(x)
85+
while len(stk) > 1:
86+
x, y = stk[-2:]
87+
g = gcd(x, y)
88+
if g == 1:
89+
break
90+
stk.pop()
91+
stk[-1] = x * y // g
92+
return stk
93+
```
94+
95+
```java
96+
class Solution {
97+
public List<Integer> replaceNonCoprimes(int[] nums) {
98+
List<Integer> stk = new ArrayList<>();
99+
for (int x : nums) {
100+
stk.add(x);
101+
while (stk.size() > 1) {
102+
x = stk.get(stk.size() - 1);
103+
int y = stk.get(stk.size() - 2);
104+
int g = gcd(x, y);
105+
if (g == 1) {
106+
break;
107+
}
108+
stk.remove(stk.size() - 1);
109+
stk.set(stk.size() - 1, (int) ((long) x * y / g));
110+
}
111+
}
112+
return stk;
113+
}
114+
115+
private int gcd(int a, int b) {
116+
if (b == 0) {
117+
return a;
118+
}
119+
return gcd(b, a % b);
120+
}
121+
}
122+
```
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<int> replaceNonCoprimes(vector<int>& nums) {
128+
vector<int> stk;
129+
for (int x : nums) {
130+
stk.push_back(x);
131+
while (stk.size() > 1) {
132+
x = stk.back();
133+
int y = stk[stk.size() - 2];
134+
int g = __gcd(x, y);
135+
if (g == 1) {
136+
break;
137+
}
138+
stk.pop_back();
139+
stk.back() = 1LL * x * y / g;
140+
}
141+
}
142+
return stk;
143+
}
144+
};
145+
```
146+
147+
```go
148+
func replaceNonCoprimes(nums []int) []int {
149+
stk := []int{}
150+
for _, x := range nums {
151+
stk = append(stk, x)
152+
for len(stk) > 1 {
153+
x = stk[len(stk)-1]
154+
y := stk[len(stk)-2]
155+
g := gcd(x, y)
156+
if g == 1 {
157+
break
158+
}
159+
stk = stk[:len(stk)-1]
160+
stk[len(stk)-1] = x * y / g
161+
}
162+
}
163+
return stk
164+
}
165+
166+
func gcd(a, b int) int {
167+
if b == 0 {
168+
return a
169+
}
170+
return gcd(b, a%b)
171+
}
172+
```
173+
174+
```ts
175+
function replaceNonCoprimes(nums: number[]): number[] {
176+
const gcd = (a: number, b: number): number => {
177+
if (b === 0) {
178+
return a;
179+
}
180+
return gcd(b, a % b);
181+
};
182+
const stk: number[] = [];
183+
for (let x of nums) {
184+
stk.push(x);
185+
while (stk.length > 1) {
186+
x = stk.at(-1)!;
187+
const y = stk.at(-2)!;
188+
const g = gcd(x, y);
189+
if (g === 1) {
190+
break;
191+
}
192+
stk.pop();
193+
stk.pop();
194+
stk.push(((x * y) / g) | 0);
195+
}
196+
}
197+
return stk;
198+
}
199+
```
200+
201+
<!-- tabs:end -->
202+
65203
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> replaceNonCoprimes(vector<int>& nums) {
4+
vector<int> stk;
5+
for (int x : nums) {
6+
stk.push_back(x);
7+
while (stk.size() > 1) {
8+
x = stk.back();
9+
int y = stk[stk.size() - 2];
10+
int g = __gcd(x, y);
11+
if (g == 1) {
12+
break;
13+
}
14+
stk.pop_back();
15+
stk.back() = 1LL * x * y / g;
16+
}
17+
}
18+
return stk;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func replaceNonCoprimes(nums []int) []int {
2+
stk := []int{}
3+
for _, x := range nums {
4+
stk = append(stk, x)
5+
for len(stk) > 1 {
6+
x = stk[len(stk)-1]
7+
y := stk[len(stk)-2]
8+
g := gcd(x, y)
9+
if g == 1 {
10+
break
11+
}
12+
stk = stk[:len(stk)-1]
13+
stk[len(stk)-1] = x * y / g
14+
}
15+
}
16+
return stk
17+
}
18+
19+
func gcd(a, b int) int {
20+
if b == 0 {
21+
return a
22+
}
23+
return gcd(b, a%b)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<Integer> replaceNonCoprimes(int[] nums) {
3+
List<Integer> stk = new ArrayList<>();
4+
for (int x : nums) {
5+
stk.add(x);
6+
while (stk.size() > 1) {
7+
x = stk.get(stk.size() - 1);
8+
int y = stk.get(stk.size() - 2);
9+
int g = gcd(x, y);
10+
if (g == 1) {
11+
break;
12+
}
13+
stk.remove(stk.size() - 1);
14+
stk.set(stk.size() - 1, (int) ((long) x * y / g));
15+
}
16+
}
17+
return stk;
18+
}
19+
20+
private int gcd(int a, int b) {
21+
if (b == 0) {
22+
return a;
23+
}
24+
return gcd(b, a % b);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
3+
stk = []
4+
for x in nums:
5+
stk.append(x)
6+
while len(stk) > 1:
7+
x, y = stk[-2:]
8+
g = gcd(x, y)
9+
if g == 1:
10+
break
11+
stk.pop()
12+
stk[-1] = x * y // g
13+
return stk

0 commit comments

Comments
 (0)