Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- /*
- *
- * no changes
- *
- int maxOnes(const vector<int>& v) {
- int n = v.size();
- int left = 0;
- int right = 0;
- int ans = 0;
- while (right < n) {
- left = right;
- while (right < n && v[right] == 1) {
- right++;
- }
- ans = max(ans, right - left);
- right++;
- }
- return ans;
- }
- */
- int maxOnes(const vector<int>& v) {
- size_t n = v.size();
- // n > 0
- // only ones no zeros
- if (count(v.begin(), v.end(), 1) == n) {
- return n - 1;
- }
- int ans = -1;
- int cur_zero_idx = -1;
- bool has_zero = false;
- int left = 0;
- int right = 0;
- while (right < n) {
- while (right < n && v[right] == 1) {
- ++right;
- }
- if (right < n) {
- has_zero = true;
- cur_zero_idx = right;
- right++; // delete zero
- while (right < n && v[right] == 1) {
- ++right;
- }
- }
- ans = max(ans, right - left);
- if (has_zero) {
- left = right = cur_zero_idx + 1; // next pos after current zero
- has_zero = false;
- }
- }
- return ans;
- }
- int main() {
- cout << maxOnes({0, 0, 1, 0, 1, 0, 0, 0, 0}) << endl;
- cout << maxOnes({1, 1, 1, 1}) << endl;
- cout << maxOnes({0, 0, 1, 1}) << endl;
- cout << maxOnes({1, 1, 1, 1, 0, 0, 0, 0, 0}) << endl;
- cout << maxOnes({0, 0, 0}) << endl;
- cout << maxOnes({1}) << endl;
- cout << maxOnes({1, 1, 1, 0, 1, 1}) << endl;
- cout << maxOnes({1,1,1,1, 0,0,0,0,0,1,1,1,1,1,1}) << endl;
- cout << maxOnes({1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1}) << endl;
- cout << maxOnes({1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1}) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement