15 Jun 2024C++ / Python / TypeScriptEasy

Max Consecutive Ones

Collected C++, Python, TypeScript solutions for max consecutive ones. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Max Consecutive Ones. the solution files are available below.

solution files

  • C++ max-consecutive-ones/synced-solution.cpp
  • Python max-consecutive-ones/synced-solution.py
  • TypeScript max-consecutive-ones/synced-solution.ts

Solution files

Pythonmax-consecutive-ones/synced-solution.py
def findMaxConsecutiveOnes(nums):
    max_count = class="syntax-number">0
    current_count = class="syntax-number">0

    for num in nums:
        if num == class="syntax-number">1:
            current_count += class="syntax-number">1
            max_count = max(max_count, current_count)
        else:
            current_count = class="syntax-number">0

    return max_count
C++max-consecutive-ones/synced-solution.cpp
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int max_count = class="syntax-number">0, current_count = class="syntax-number">0;

        for (int num : nums) {
            if (num == class="syntax-number">1) {
                current_count++;
                max_count = max(max_count, current_count);
            } else {
                current_count = class="syntax-number">0;
            }
        }

        return max_count;
    }
};
TypeScriptmax-consecutive-ones/synced-solution.ts
function findMaxConsecutiveOnes(nums: number[]): number {
    let maxCount = class="syntax-number">0, currentCount = class="syntax-number">0;

    for (const num of nums) {
        if (num === class="syntax-number">1) {
            currentCount++;
            maxCount = Math.max(maxCount, currentCount);
        } else {
            currentCount = class="syntax-number">0;
        }
    }

    return maxCount;
}