[InterviewBit] Max Continuous Series of 1s

Max Continuous Series of 1s

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vector<int> Solution::maxone(vector<int> &A, int B) {
int l = 0, r = 0, n = A.size(), now = 0;
int len = 0, p = 0;
while(r < n) {
while(r < n and now <= B) {
if(r - l > len) {
len = r - l;
p = l;
}
if(A[r++] == 0) now += 1;
}
if(now <= B and r - l > len) {
len = r - l;
p = l;
}
while(r < n and now > B) {
if(A[l++] == 0) now -= 1;
}
}
vector<int> res;
for(int i = p; i < p + len; i++) res.push_back(i);
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/06/PS/interviewbit/max-continuous-series-of-1s/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.