-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (73 loc) · 2.03 KB
/
main.go
File metadata and controls
80 lines (73 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Source: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i
// Title: Minimum Operations to Make Binary Array Elements Equal to One I
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a binary array `nums`.
//
// A **binary array** is an array which contains only 0 and 1.
//
// You can do the following operation on the array **any** number of times (possibly zero):
//
// - Choose **any** 3 **consecutive** elements from the array and **flip** **all** of them.
//
// **Flipping** an element means changing its value from 0 to 1, and from 1 to 0.
//
// Return the **minimum** number of operations required to make all elements in `nums` equal to 1. If it is impossible, return -1.
//
// **Example 1:**
//
// ```
// Input: nums = [0,1,1,1,0,0]
//
// Output: 3
//
// Explanation:
// We can do the following operations:
//
// - Choose the elements at indices 0, 1 and 2. The resulting array is `nums = [**1**,**0**,**0**,1,0,0]`.
// - Choose the elements at indices 1, 2 and 3. The resulting array is `nums = [1,**1**,**1**,**0**,0,0]`.
// - Choose the elements at indices 3, 4 and 5. The resulting array is `nums = [1,1,1,**1**,**1**,**1**]`.
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [0,1,1,1]
//
// Output: -1
//
// Explanation:
// It is impossible to make all elements equal to 1.
// ```
//
// **Constraints:**
//
// - `3 <= nums.length <= 10^5`
// - `0 <= nums[i] <= 1`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import "fmt"
func minOperations(nums []int) int {
n := len(nums)
res := 0
for i := range n - 2 {
if nums[i] == 1 {
continue
}
res++
// nums[i+0] = 1
nums[i+1] ^= 1
nums[i+2] ^= 1
}
if nums[n-2] == 0 || nums[n-1] == 0 {
return -1
}
return res
}
func main() {
fmt.Println(minOperations([]int{
0, 1, 1, 1,
}))
}