Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.51 KB

_3192. Minimum Operations to Make Binary Array Elements Equal to One II.md

File metadata and controls

62 lines (45 loc) · 1.51 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 : July 04, 2024


Related Topics : Array, Dynamic Programming, Greedy

Acceptance Rate : 65.49 %


Sigh. I decided I should rest today so I didn't wake up for 
the contest, choosing to just do the weekly one. Then when I woke 
up and was like let's try the questions out, I finished all 3 
in sub-20 minutes. Welp. /shrug 

Solutions

Java

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

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

        for (int i = 0; i < nums.length; i++) {
            int current = flipped ? hasmap[nums[i]] : nums[i];
            if (current == 0) {
                turns += 1;
                flipped = !flipped;
            }
        }

        if (!flipped
            && nums[nums.length - 1] == 1)
            return turns;

        else if (nums[nums.length - 1] == 0)
            return turns;

        return -1;   
    }
}