https://leetcode.cn/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/
还是做题太少,思维能力不太够。
想了好久,看到题解后,觉得这种解决问题的方法需要记录一下。
class Solution {
public:
int minOperations(vector<int>& nums) {
int res = 0;
int n = nums.size();
for (int i = 0; i < n; i++) {
if (nums[i] == 1) continue;
if (i + 1 >= n || i + 2 >= n) return -1;
nums[i+1] ^= 1;
nums[i+2] ^= 1;
res++;
}
return res;
}
};