Skip to content

Commit e4c408e

Browse files
committed
Add solution and test-cases for problem 2654
1 parent f03226b commit e4c408e

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [2654.Minimum Number of Operations to Make All Array Elements Equal to 1][title]
2+
3+
## Description
4+
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:
5+
6+
- Select an index `i` such that `0 <= i < n - 1` and replace either of `nums[i]` or `nums[i+1]` with their gcd value.
7+
8+
Return the **minimum** number of operations to make all elements of `nums` equal to `1`. If it is impossible, return `-1`.
9+
10+
The gcd of two integers is the greatest common divisor of the two integers.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [2,6,3,4]
16+
Output: 4
17+
Explanation: We can do the following operations:
18+
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
19+
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
20+
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
21+
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: nums = [2,10,6,14]
28+
Output: -1
29+
Explanation: It can be shown that it is impossible to make all the elements equal to 1.
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func gcd2654(a, b int) int {
4+
for b != 0 {
5+
a, b = b, a%b
6+
}
7+
return a
8+
}
9+
10+
func Solution(nums []int) int {
11+
l := len(nums)
12+
ones := 0
13+
// 有1就是直接响四周扩散就可以
14+
for _, n := range nums {
15+
if n == 1 {
16+
ones++
17+
}
18+
}
19+
if ones > 0 {
20+
return l - ones
21+
}
22+
23+
// 没有,就需要看否构建出1
24+
minLenArrayWithOne := l + 1
25+
// 2, 2, 3, 4, 5
26+
for i := 0; i < l; i++ {
27+
gcdNum := nums[i]
28+
for j := i + 1; j < l; j++ {
29+
gcdNum = gcd2654(gcdNum, nums[j])
30+
if gcdNum == 1 {
31+
minLenArrayWithOne = min(minLenArrayWithOne, j-i+1)
32+
break
33+
}
34+
}
35+
}
36+
if minLenArrayWithOne == l+1 {
37+
return -1
38+
}
39+
// 构建minLenArrayWithOne里面的1,需要-1次,然后扩散到整个数组
40+
return minLenArrayWithOne - 1 + l - 1
541
}

leetcode/2601-2700/2654.Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/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{2, 6, 3, 4}, 4},
17+
{"TestCase2", []int{2, 10, 6, 14}, -1},
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)