Skip to content

Commit 5caf6de

Browse files
authored
Merge pull request #1154 from 0xff-dev/3191
Add solution and test-cases for problem 3191
2 parents a366b26 + ef22f1a commit 5caf6de

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

leetcode/3101-3200/3191.Minimum-Operations-to-Make-Binary-Array-Elements-Equal-to-One-I/README.md

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [3191.Minimum Operations to Make Binary Array Elements Equal to One I][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 a `binary array` `nums`.
5+
6+
You can do the following operation on the array **any** number of times (possibly zero):
7+
8+
- Choose **any 3 consecutive** elements from the array and **flip all** of them.
9+
10+
**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.
11+
12+
Return the **minimum** number of operations required to make all elements in `nums` equal to 1. If it is impossible, return -1.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
17+
Input: nums = [0,1,1,1,0,0]
1418
15-
## 题意
16-
> ...
19+
Output: 3
1720
18-
## 题解
21+
Explanation:
22+
We can do the following operations:
1923
20-
### 思路1
21-
> ...
22-
Minimum Operations to Make Binary Array Elements Equal to One I
23-
```go
24+
Choose the elements at indices 0, 1 and 2. The resulting array is nums = [1,0,0,1,0,0].
25+
Choose the elements at indices 1, 2 and 3. The resulting array is nums = [1,1,1,0,0,0].
26+
Choose the elements at indices 3, 4 and 5. The resulting array is nums = [1,1,1,1,1,1].
2427
```
2528

29+
**Example 2:**
30+
31+
```
32+
Input: nums = [0,1,1,1]
33+
34+
Output: -1
35+
36+
Explanation:
37+
It is impossible to make all elements equal to 1.
38+
```
2639

2740
## 结语
2841

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := 0
5+
index := 0
6+
l := len(nums)
7+
for ; index < l; index++ {
8+
if nums[index] == 1 {
9+
continue
10+
}
11+
if index >= l-2 {
12+
return -1
13+
}
14+
nums[index] = 1 - nums[index]
15+
nums[index+1] = 1 - nums[index+1]
16+
nums[index+2] = 1 - nums[index+2]
17+
ans++
18+
}
19+
return ans
520
}

leetcode/3101-3200/3191.Minimum-Operations-to-Make-Binary-Array-Elements-Equal-to-One-I/Solution_test.go

+6-7
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{0, 1, 1, 1, 0, 0}, 3},
17+
{"TestCase2", []int{0, 1, 1, 1}, -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)