Leetcode 485 Solution

This article provides solution to leetcode question 485 (max-consecutive-ones)

https://leetcode.com/problems/max-consecutive-ones

Solution

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int max_cnt = 0;
        int cnt = 0;
        nums.push_back(0);

        for (auto num : nums)
            if (num == 1)
                cnt++;
            else
            {
                max_cnt = max(max_cnt, cnt);
                cnt = 0;
            }

        return max_cnt;
    }
};