-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3192-MinimumOperationsToMakeBinaryArrayElementsEqualToOneII.go
More file actions
97 lines (83 loc) · 2.92 KB
/
3192-MinimumOperationsToMakeBinaryArrayElementsEqualToOneII.go
File metadata and controls
97 lines (83 loc) · 2.92 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
// 3192. Minimum Operations to Make Binary Array Elements Equal to One II
// You are given a binary array nums.
// You can do the following operation on the array any number of times (possibly zero):
// Choose any index i from the array and flip all the elements from index i to the end of the array.
// 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.
// Example 1:
// Input: nums = [0,1,1,0,1]
// Output: 4
// Explanation:
// We can do the following operations:
// Choose the index i = 1. The resulting array will be nums = [0,0,0,1,0].
// Choose the index i = 0. The resulting array will be nums = [1,1,1,0,1].
// Choose the index i = 4. The resulting array will be nums = [1,1,1,0,0].
// Choose the index i = 3. The resulting array will be nums = [1,1,1,1,1].
// Example 2:
// Input: nums = [1,0,0,0]
// Output: 1
// Explanation:
// We can do the following operation:
// Choose the index i = 1. The resulting array will be nums = [1,1,1,1].
// Constraints:
// 1 <= nums.length <= 10^5
// 0 <= nums[i] <= 1
import "fmt"
func minOperations(nums []int) int {
res, flipped := 0, 0
for i := 0; i < len(nums); i++ {
// Determine the current state considering the number of flips so far
state := nums[i]
if flipped % 2 != 0 { state = 1 - nums[i] }
if state == 0 { // Perform a flip operation
res++
flipped++
}
}
return res
}
func minOperations1(nums []int) int {
res := 0
for i := len(nums) - 2; i >= 0; i-- {
if nums[i] != nums[i+1] {
res++
}
}
if nums[0] == 1 { return res }
return res + 1
}
func minOperations2(nums []int) int {
res, val := 0, 0
for _, v := range nums {
v ^= val
if v == 0 {
val ^= 1
res++
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [0,1,1,0,1]
// Output: 4
// Explanation:
// We can do the following operations:
// Choose the index i = 1. The resulting array will be nums = [0,0,0,1,0].
// Choose the index i = 0. The resulting array will be nums = [1,1,1,0,1].
// Choose the index i = 4. The resulting array will be nums = [1,1,1,0,0].
// Choose the index i = 3. The resulting array will be nums = [1,1,1,1,1].
fmt.Println(minOperations([]int{0,1,1,0,1})) // 4
// Example 2:
// Input: nums = [1,0,0,0]
// Output: 1
// Explanation:
// We can do the following operation:
// Choose the index i = 1. The resulting array will be nums = [1,1,1,1].
fmt.Println(minOperations([]int{1,0,0,0})) // 1
fmt.Println(minOperations1([]int{0,1,1,0,1})) // 4
fmt.Println(minOperations1([]int{1,0,0,0})) // 1
fmt.Println(minOperations2([]int{0,1,1,0,1})) // 4
fmt.Println(minOperations2([]int{1,0,0,0})) // 1
}