Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 846 Bytes

Question_3192.md

File metadata and controls

35 lines (30 loc) · 846 Bytes

LeetCode Records - Question 3192 Minimum Operations to Make Binary Array Elements Equal to One II

Attempt 1: Start from the first zero and count the number of different groups

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

        int i = 0;
        while (i < nums.length) {
            while (i < nums.length && nums[i] == 1) {
                i++;
            }
            if (i >= nums.length) {
                return count;
            }
            count++;

            while (i < nums.length && nums[i] == 0) {
                i++;
            }
            if (i >= nums.length) {
                return count;
            }
            count++;
        }

        return count;
    }
}
  • Runtime: 6 ms (Beats: 99.18%)
  • Memory: 57.04 MB (Beats: 42.80%)