Minimum Operations to Make Binary Array Elements Equal to One I

Medium Topics Company Tags

In one operation, choose exactly three consecutive entries of the binary array nums and flip all three bits. Return the fewest operations needed to make every entry equal to 1, or -1 if this is impossible.

Example 1:

Input: nums = [0,0,0]

Output: 1

Explanation: Flip the only group of three entries to change [0,0,0] into [1,1,1].

Example 2:

Input: nums = [1,1,1]

Output: 0

Explanation: All entries are already 1, so no operation is needed.

Example 3:

Input: nums = [1,0,0,0]

Output: 1

Explanation: Flip the last three entries once, obtaining [1,1,1,1].

Constraints:

  • 3 <= nums.length <= 100000
  • 0 <= nums[i] <= 1


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.