aboutsummaryrefslogtreecommitdiff
path: root/min_ops_bin_arr_1.py
blob: eed3fea7fbe6ab4314471274731a276d9a146278 (plain)
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
# 3191. Minimum Operations to Make Binary Array Elements Equal to One I

"""
you are given a binary array 'nums'. you can do the following operation on
the array any number of times (possibly zero): choose any three consecutive
elements from the array and flip all of them. flipping an element means
changing its value from 0 to 1, and from 1 to 0. return the minimum number of
operations required needed to make all elements in 'nums' equal to 1. if it
is impossible, return -1.
"""


class Solution(object):
    def minOperations(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        cnt, n = 0, len(nums)
        for i in range(n - 2):
            if nums[i] == 0:
                nums[i] ^= 1
                nums[i + 1] ^= 1
                nums[i + 2] ^= 1
                cnt += 1
        return cnt if nums[n - 2] == 1 and nums[n - 1] == 1 else -1


if __name__ == "__main__":
    obj = Solution()
    print(obj.minOperations(nums=[0, 1, 1, 1, 0, 0]))
    print(obj.minOperations(nums=[0, 1, 1, 1]))