Skip to content

Commit b6abb1e

Browse files
Create max_consecutive_ones.cpp
1 parent 9217ba7 commit b6abb1e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

max_consecutive_ones.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int findMaxConsecutiveOnes(vector<int>& nums) {
4+
int cnt = 0, mxc = INT_MIN;
5+
6+
for(int i=0; i<nums.size(); i++){
7+
if(nums[i] == 1){
8+
cnt++;
9+
mxc = max(cnt, mxc);
10+
}
11+
else{
12+
cnt = 0;
13+
}
14+
15+
}
16+
if(mxc == INT_MIN)
17+
return nums[0];
18+
else
19+
return mxc;
20+
}
21+
};

0 commit comments

Comments
 (0)