-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidingwindow.cpp
More file actions
35 lines (32 loc) · 807 Bytes
/
Copy pathslidingwindow.cpp
File metadata and controls
35 lines (32 loc) · 807 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
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int longestOnes(vector<int>& nums, int k) {
int r=0;
int l=0;
int n=nums.size();
int maxlen=0;
int temp=k;
while(r<n){
if(nums[r]==1){
maxlen=max(maxlen,r-l+1);
r++;
}
else{
if(temp!=0){
maxlen=max(maxlen,r-l+1);
r++;
temp=temp-1;
}
else{
while(l<=r && temp==0){
if(nums[l]==0){
temp++;
}
l++;
}
}
}
}
return maxlen;
}
};