485. Max Consecutive Ones

problem

solution

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count = 0, ret = 0;
for(int n:nums){
if(n==1) count++;
else {ret= max(ret, count); count= 0;}
}
return max(ret, count);
}
};

analysis

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