Skip to content

Files

Latest commit

 

History

History
50 lines (37 loc) · 1.34 KB

_3191. Minimum Operations to Make Binary Array Elements Equal to One I.md

File metadata and controls

50 lines (37 loc) · 1.34 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum

Acceptance Rate : 70.24 %


Solutions

Java

class Solution {
    public int minOperations(int[] nums) {
        int turns = 0;

        int[] hasmap = new int[]{1, 0};

        for (int i = 0; i < nums.length - 2; i++) {
            if (nums[i] == 0) {
                turns += 1;
                nums[i] = hasmap[nums[i]];
                nums[i + 1] = hasmap[nums[i + 1]];
                nums[i + 2] = hasmap[nums[i + 2]];
            }
        }

        if (nums[nums.length - 1] == 1 
            && nums[nums.length - 2] == 1
            && nums[nums.length - 3] == 1)
            return turns;
        return -1;   
    }
}