-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1004.cpp
42 lines (40 loc) · 1004 Bytes
/
P1004.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
#include "header.h"
class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int h,t;
h=t=0;
int k=0, ans=0;
while (t<A.size()) {
if (A[t]==1) {
t++;
}
else if (k<K) {
k++;
t++;
} else {
if (A[h]==0) k--;
h++;
}
ans = max(ans, t - h);
}
return ans;
}
};
int main() {
vector<int> A;
int K;
A = {1,1,1,0,0,0,1,1,1,1,0}; K=2;
cout << Solution().longestOnes(A, K) << endl;
A = {0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1}; K=3;
cout << Solution().longestOnes(A, K) << endl;
A = {0,0}; K=2;
cout << Solution().longestOnes(A, K) << endl;
A = {0}; K=2;
cout << Solution().longestOnes(A, K) << endl;
A = {}; K=2;
cout << Solution().longestOnes(A, K) << endl;
A = {1,1,1,1,1}; K=2;
cout << Solution().longestOnes(A, K) << endl;
return 0;
}