1869. Longer Contiguous Segments of Ones than Zeros

problem

solution

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
class Solution {
public:
bool checkZeroOnes(string s) {
int ones = 0, zeros = 0;
int count0 = 0, count1= 0;
for(char c:s){
if(c=='1'){
count1++;

zeros = max(zeros, count0);
count0 = 0;

}
else{
count0++;

ones = max(ones, count1);
count1 = 0;
}
}
ones = max(ones, count1);
zeros = max(zeros, count0);
return ones>zeros?true:false;

}
};

analysis

  • time complexity O(n)
  • space complexity O(1)