Minimum Operations to Make Binary Array Elements Equal to One I
You are given a binary array `nums`. In one operation you may choose any 3 consecutive elements and flip all of them (0 becomes 1, 1 becomes 0). Return the minimum number of operations needed to make every element 1, or `-1` if it is impossible.
30:00
Minimum Operations to Make Binary Array Elements Equal to One I
You are given a binary array nums. In one operation you may choose any 3 consecutive elements and flip all of them (0 becomes 1, 1 becomes 0). Return the minimum number of operations needed to make every element 1, or -1 if it is impossible.
Example 1:
Input: {"nums":[0,1,1,1,0,0]}
Output: 3
Constraints:
3≤nums.length≤105
0≤nums[i]≤1
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[0,1,1,1,0,0]}
Scan left to right. Once the stamp moves past index i, nothing can change nums[i] again, so a 0 at the stamp's left edge must be flipped right now.
nums, 3-wide flip stamp
0
1
1
1
0
0
0
1
2
3
4
5
green edge = fixed for good; last two = no stamp can start there