DDSA
Advertisement

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

Time: O(n)
Space: O(1)

Approach

Greedy sliding window of size 3; flip starting at each 0.

3191.cs
C#
// Approach: Greedy sliding window of size 3; flip starting at each 0.
// Time: O(n) Space: O(1)

public class Solution
{
    public int MinOperations(int[] nums)
    {
        int n = nums.Length, ans = 0;

        for (int i = 0; i < n - 2; i++)
        {
            if (nums[i] == 1)
                continue;

            for (int j = i; j <= i + 2; j++)
                nums[j] = nums[j] ^ 1;

            ans++;
        }

        if (nums[n - 1] == 0 || nums[n - 2] == 0)
            return -1;

        return ans;
    }
}
Advertisement
Was this solution helpful?