URL: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/ Signed-off-by: Matej Focko <me@mfocko.xyz>
22 lines
311 B
Go
22 lines
311 B
Go
package main
|
|
|
|
func minOperations(nums []int) int {
|
|
n := len(nums)
|
|
|
|
count := 0
|
|
for i := 0; i < n-2; i++ {
|
|
if nums[i] != 0 {
|
|
continue
|
|
}
|
|
|
|
nums[i] = 1
|
|
nums[i+1] = (1 + nums[i+1]) % 2
|
|
nums[i+2] = (1 + nums[i+2]) % 2
|
|
count++
|
|
}
|
|
|
|
if nums[n-2] == 0 || nums[n-1] == 0 {
|
|
return -1
|
|
}
|
|
return count
|
|
}
|