Easy C++ O(n) solution.


  • 0
    S

    class Solution {
    public:
    int findMaxConsecutiveOnes(vector<int>& nums) {

        int max = INT_MIN, c = 1,  n = nums.size();
        int count[256] = {0};
        
        for(int i = 0; i<n;i++) count[nums[i]]++;
        
        if(n == 1) return nums[0];
        for(int i= 0;i<n-1;i++)
        {
            if(nums[i] == nums[i+1] && nums[i] == 1)
            {
                c++;
                if(max<c)
                    max = c;
            }
            else
            {
                
                c = 1;
            }
        }
        
        if(max>0)
            return max;
        if(count[1] == 1)
            return 1;
        if(count[1] == 0)
            return 0;
        return c;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.