-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3666.cpp
More file actions
26 lines (25 loc) · 706 Bytes
/
3666.cpp
File metadata and controls
26 lines (25 loc) · 706 Bytes
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
class Solution {
public:
int minOperations(string s, int k) {
long long ones = 0;
long long zeros = 0;
for (auto& c : s) {
if (c == '0') zeros++;
else ones++;
}
if (zeros == 0) return 0;
for (int i = 1; i <= s.size(); ++i) {
long long quota = i * k;
long long valid = quota - zeros;
if (valid & 1) continue;
if (quota < zeros) continue;
if (i & 1) {
if (quota <= zeros * i + ones * (i - 1)) return i;
}
else {
if (quota <= ones * i + zeros * (i - 1)) return i;
}
}
return -1;
}
};