-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (61 loc) · 1.61 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
public:
int minKBitFlips(vector<int>& nums, int k) {
int n = nums.size();
int count = 0;
bool flipped = false;
vector<bool> flip(n, false);
for (int i = 0; i < nums.size(); ++i) {
if (i >= k) flipped ^= flip[i - k];
if (flipped != nums[i]) continue;
if (i + k > n) return -1;
flip[i] = true;
flipped = !flipped;
++count;
}
return count;
}
};
class Solution {
public:
int minKBitFlips(vector<int>& nums, int k) {
int n = nums.size();
int count = 0;
bool flipped = false;
deque<bool> flip;
for (int i = 0; i < nums.size(); ++i) {
if (i >= k) {
flipped ^= flip.front();
flip.pop_front();
}
flip.push_back(false);
if (flipped != nums[i]) continue;
if (i + k > n) return -1;
flip.back() = true;
flipped = !flipped;
++count;
}
return count;
}
};
class Solution {
public:
int minKBitFlips(vector<int>& nums, int k) {
int n = nums.size();
int count = 0;
bool flipped = false;
for (int i = 0; i < n; ++i) {
if (i >= k) flipped ^= nums[i - k] > 1;
if (flipped != nums[i]) continue;
if (i + k > n) {
count = -1;
break;
}
nums[i] += 2;
flipped = !flipped;
++count;
}
for (int& v : nums) v %= 2;
return count;
}
};