3191.cpp (401B)
1 class Solution { 2 public: 3 int minOperations(vector<int> &nums) const { 4 const int n = size(nums); 5 int res = 0; 6 7 for (int i = 0; i < n - 2; i++) { 8 if (nums[i]) continue; 9 nums[i + 1] = !nums[i + 1]; 10 nums[i + 2] = !nums[i + 2]; 11 res++; 12 } 13 14 if (!nums[n - 2] || !nums[n - 1]) return -1; 15 return res; 16 } 17 };