Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Max Consecutive Ones


Solution


    class Solution {
    public:
        int findMaxConsecutiveOnes(vector<int>& nums) {
            int count = 0;
            int maxOnes = 0;
            for(int i = 0; i < nums.size(); i++) {
                if(nums[i] == 1)
                    count++;
                else {
                    maxOnes = max(maxOnes, count);
                    count = 0;
                }
            }
            maxOnes = max(maxOnes, count);
            return maxOnes;
        }
    };