Skip to content
LC-3191 Medium LeetCode

3191. Minimum Operations to Make Binary Array Elements Equal to One I

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 81% Topics: Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# greedy
class Solution(object):
    def minOperations(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result = 0
        for i in xrange(len(nums)-2):
            if nums[i]:
                continue
            nums[i+1] ^= 1
            nums[i+2] ^= 1
            result += 1
        return result if nums[-2] == nums[-1] == 1 else -1

Solution from kamyu104/LeetCode-Solutions · MIT