Skip to content

Commit 23fff8f

Browse files
authored
Merge pull request #1315 from 0xff-dev/2197
Add solution and test-cases for problem 2197
2 parents 06dad5e + 582334a commit 23fff8f

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/README.md

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [2197.Replace Non-Coprime Numbers in Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an array of integers `nums`. Perform the following steps:
5+
6+
1. Find **any** two **adjacent** numbers in nums that are **non-coprime**.
7+
2. If no such numbers are found, **stop** the process.
8+
3. Otherwise, delete the two numbers and **replace** them with their **LCM (Least Common Multiple)**.
9+
4. **Repeat** this process as long as you keep finding two adjacent non-coprime numbers.
10+
11+
Return the **final** modified array. It can be shown that replacing adjacent non-coprime numbers in **any** arbitrary order will lead to the same result.
12+
13+
The test cases are generated such that the values in the final array are **less than or equal to 10^8**.
14+
15+
Two values `x` and `y` are **non-coprime** if `GCD(x, y) > 1` where `GCD(x, y)` is the **Greatest Common Divisor** of `x` and `y`.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: nums = [6,4,3,2,7,6,2]
21+
Output: [12,7,6]
22+
Explanation:
23+
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [12,3,2,7,6,2].
24+
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [12,2,7,6,2].
25+
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [12,7,6,2].
26+
- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,6].
27+
There are no more adjacent non-coprime numbers in nums.
28+
Thus, the final modified array is [12,7,6].
29+
Note that there are other ways to obtain the same resultant array.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 2:**
1933

20-
### 思路1
21-
> ...
22-
Replace Non-Coprime Numbers in Array
23-
```go
2434
```
25-
35+
Input: nums = [2,2,1,1,3,3,3]
36+
Output: [2,1,1,3]
37+
Explanation:
38+
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3,3].
39+
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3].
40+
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [2,1,1,3].
41+
There are no more adjacent non-coprime numbers in nums.
42+
Thus, the final modified array is [2,1,1,3].
43+
Note that there are other ways to obtain the same resultant array.
44+
```
2645

2746
## 结语
2847

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func gcd2197(a, b int) int {
4+
for b != 0 {
5+
a, b = b, a%b
6+
}
7+
return a
8+
}
9+
10+
func lcm2197(a, b, gcd int) int {
11+
return a / gcd * b
12+
}
13+
14+
func Solution(nums []int) []int {
15+
l := len(nums)
16+
stack := make([]int, l)
17+
stack[0] = nums[0]
18+
index := 0
19+
for i := 1; i < len(nums); i++ {
20+
g := gcd2197(stack[index], nums[i])
21+
// 如果可以一只gcd下去,那就一只gcd2197
22+
cmp := nums[i]
23+
for ; index >= 0; index-- {
24+
g = gcd2197(stack[index], cmp)
25+
if g == 1 {
26+
break
27+
}
28+
cmp = lcm2197(stack[index], cmp, g)
29+
}
30+
index++
31+
stack[index] = cmp
32+
}
33+
return stack[:index+1]
534
}

leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{6, 4, 3, 2, 7, 6, 2}, []int{12, 7, 6}},
17+
{"TestCase2", []int{2, 2, 1, 1, 3, 3, 3}, []int{2, 1, 1, 3}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)