diff --git a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/README.md b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/README.md index 63ee94526..0fc2949ba 100755 --- a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/README.md +++ b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/README.md @@ -1,28 +1,47 @@ # [2197.Replace Non-Coprime Numbers in Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an array of integers `nums`. Perform the following steps: + +1. Find **any** two **adjacent** numbers in nums that are **non-coprime**. +2. If no such numbers are found, **stop** the process. +3. Otherwise, delete the two numbers and **replace** them with their **LCM (Least Common Multiple)**. +4. **Repeat** this process as long as you keep finding two adjacent non-coprime numbers. + +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. + +The test cases are generated such that the values in the final array are **less than or equal to 10^8**. + +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [6,4,3,2,7,6,2] +Output: [12,7,6] +Explanation: +- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [12,3,2,7,6,2]. +- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [12,2,7,6,2]. +- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [12,7,6,2]. +- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,6]. +There are no more adjacent non-coprime numbers in nums. +Thus, the final modified array is [12,7,6]. +Note that there are other ways to obtain the same resultant array. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Replace Non-Coprime Numbers in Array -```go ``` - +Input: nums = [2,2,1,1,3,3,3] +Output: [2,1,1,3] +Explanation: +- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3,3]. +- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3]. +- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [2,1,1,3]. +There are no more adjacent non-coprime numbers in nums. +Thus, the final modified array is [2,1,1,3]. +Note that there are other ways to obtain the same resultant array. +``` ## 结语 diff --git a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution.go b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution.go index d115ccf5e..cbfb52643 100644 --- a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution.go +++ b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution.go @@ -1,5 +1,34 @@ package Solution -func Solution(x bool) bool { - return x +func gcd2197(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} + +func lcm2197(a, b, gcd int) int { + return a / gcd * b +} + +func Solution(nums []int) []int { + l := len(nums) + stack := make([]int, l) + stack[0] = nums[0] + index := 0 + for i := 1; i < len(nums); i++ { + g := gcd2197(stack[index], nums[i]) + // 如果可以一只gcd下去,那就一只gcd2197 + cmp := nums[i] + for ; index >= 0; index-- { + g = gcd2197(stack[index], cmp) + if g == 1 { + break + } + cmp = lcm2197(stack[index], cmp, g) + } + index++ + stack[index] = cmp + } + return stack[:index+1] } diff --git a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution_test.go b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution_test.go index 14ff50eb4..f42cb861b 100644 --- a/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution_test.go +++ b/leetcode/2101-2200/2197.Replace-Non-Coprime-Numbers-in-Array/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{6, 4, 3, 2, 7, 6, 2}, []int{12, 7, 6}}, + {"TestCase2", []int{2, 2, 1, 1, 3, 3, 3}, []int{2, 1, 1, 3}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }