C++ O(n) Time O(1) Space Solution


  • 0
    I
    class Solution {
    public:
        int findMaxConsecutiveOnes(vector<int>& nums) {
            size_t g = 0;
            size_t f = 0;
            size_t max_con = 0;
    
            for (size_t i = 0; i < nums.size(); i++) {
                if (nums[i] == 1) {
                    g++;
                    f++;
                } else {
                    f = g + 1;
                    g = 0;
                }
                
                max_con = std::max(max_con, f);
            }
    
    		return max_con;
        }
    };
    

Log in to reply
 

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