|
1 | 1 | # [2197.Replace Non-Coprime Numbers in Array][title] |
2 | 2 |
|
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 | | -
|
6 | 3 | ## 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`. |
7 | 16 |
|
8 | 17 | **Example 1:** |
9 | 18 |
|
10 | 19 | ``` |
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. |
13 | 30 | ``` |
14 | 31 |
|
15 | | -## 题意 |
16 | | -> ... |
17 | | -
|
18 | | -## 题解 |
| 32 | +**Example 2:** |
19 | 33 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Replace Non-Coprime Numbers in Array |
23 | | -```go |
24 | 34 | ``` |
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 | +``` |
26 | 45 |
|
27 | 46 | ## 结语 |
28 | 47 |
|
|
0 commit comments