go: add «3191. Minimum Operations to Make Binary Array Elements Equal to One I»
URL: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
790eeb39ef
commit
b876b68ef1
1 changed files with 22 additions and 0 deletions
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue