Beats 94.73% O(n) c++ solution


  • 0
    Y
    int findMaxConsecutiveOnes(vector<int>& nums) {
            vector<int> index;
            for(int i = 0; i < nums.size() ; i ++){
                if(nums[i] != 1)
                    index.push_back(i);
            }
            
            if(index.size() == 0)
                return nums.size();
            
            int max = nums.size() - index[index.size() - 1] - 1;
            for(int i = 0; i < index.size() - 1; i ++){
                if(max < index[i+1] - index[i] - 1 )
                    max = (index[i+1] - index[i] - 1);
            }
            
            if(max < index[0])
                max = index[0];
            return max;
        }
    

Log in to reply
 

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