[InterviewBit] Maximum Ones After Modification

Maximum Ones After Modification

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Solution::solve(vector<int> &A, int B) {
int res = 0, l = 0, r = 0, n = A.size(), now = 0;
while(r < n) {
while(r < n and now <= B) {
res = max(res, r - l);
if(A[r++] == 0) now += 1;
}
if(now <= B) res = max(res, r - l);
while(now > B) {
if(A[l++] == 0) now -= 1;
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/31/PS/interviewbit/maximum-ones-after-modification/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.