Intuitive O(N) Solution in C++


  • 0
    J
        int findMaxConsecutiveOnes(vector<int>& nums) {
            
            int count = 0;
            int max = 0;
            
            for (int i = 0; i < nums.size(); i++) {
                if (nums[i] == 0) {
                    if (count > max) {
                        max = count;
                    }
                    count = 0;
                } else {
                    count++;
                }
            }
            
            if (count > max) {
                max = count;
            }
            
            return max;
            
        }

Log in to reply
 

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