diff --git a/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/README.md b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/README.md new file mode 100644 index 000000000..572fc0897 --- /dev/null +++ b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/README.md @@ -0,0 +1,37 @@ +# [2654.Minimum Number of Operations to Make All Array Elements Equal to 1][title] + +## Description +You are given a **0-indexed** array `nums` consisiting of **positive** integers. You can do the following operation on the array **any** number of times: + +- Select an index `i` such that `0 <= i < n - 1` and replace either of `nums[i]` or `nums[i+1]` with their gcd value. + +Return the **minimum** number of operations to make all elements of `nums` equal to `1`. If it is impossible, return `-1`. + +The gcd of two integers is the greatest common divisor of the two integers. + +**Example 1:** + +``` +Input: nums = [2,6,3,4] +Output: 4 +Explanation: We can do the following operations: +- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4]. +- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4]. +- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4]. +- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1]. +``` + +**Example 2:** + +``` +Input: nums = [2,10,6,14] +Output: -1 +Explanation: It can be shown that it is impossible to make all the elements equal to 1. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution.go b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution.go index d115ccf5e..297260c8d 100755 --- a/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution.go +++ b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution.go @@ -1,5 +1,41 @@ package Solution -func Solution(x bool) bool { - return x +func gcd2654(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} + +func Solution(nums []int) int { + l := len(nums) + ones := 0 + // 有1就是直接响四周扩散就可以 + for _, n := range nums { + if n == 1 { + ones++ + } + } + if ones > 0 { + return l - ones + } + + // 没有,就需要看否构建出1 + minLenArrayWithOne := l + 1 + // 2, 2, 3, 4, 5 + for i := 0; i < l; i++ { + gcdNum := nums[i] + for j := i + 1; j < l; j++ { + gcdNum = gcd2654(gcdNum, nums[j]) + if gcdNum == 1 { + minLenArrayWithOne = min(minLenArrayWithOne, j-i+1) + break + } + } + } + if minLenArrayWithOne == l+1 { + return -1 + } + // 构建minLenArrayWithOne里面的1,需要-1次,然后扩散到整个数组 + return minLenArrayWithOne - 1 + l - 1 } diff --git a/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution_test.go b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution_test.go index 14ff50eb4..7ebc1f339 100755 --- a/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/Solution_test.go +++ b/leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/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{2, 6, 3, 4}, 4}, + {"TestCase2", []int{2, 10, 6, 14}, -1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }