Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 485 · Linear Scan

Max Consecutive Ones

LeetCode 485 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Running streak, reset on non-1.

సులభంArray
LeetCode లో తెరవండి ↗

నేను Max Consecutive Ones (LeetCode 485) ను ఇలా పరిష్కరించాను — Linear Scan ప్యాటర్న్‌లోని ఒక సులభం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Keep a running count of the current streak of 1s; reset it to 0 at any other value, and remember the longest streak seen.

Approach

  1. 1

    count = 0, maxLength = 0.

  2. 2

    1 → count++; otherwise count = 0.

  3. 3

    Update maxLength each step.

నేను ఎలా పరిష్కరించాను

Running countPythonO(n) time · O(1) space
class Solution:
    def findMaxConsecutiveOnes(self, nums):
        maxLength = 0
        count = 0
        for i in range(len(nums)):
            if nums[i] != 1:
                count = 0
            else:
                count += 1
            maxLength = max(maxLength, count)
        return maxLength
స్టెప్-త్రూ లోడ్ అవుతోంది…

Max Consecutive Ones — తరచూ అడిగే ప్రశ్నలు

Max Consecutive Ones వెనుక ఉన్న intuition ఏమిటి?

Keep a running count of the current streak of 1s; reset it to 0 at any other value, and remember the longest streak seen.

Max Consecutive Ones (LeetCode 485) ను ఎలా పరిష్కరించాలి?

1. count = 0, maxLength = 0. 2. 1 → count++; otherwise count = 0. 3. Update maxLength each step.

Max Consecutive Ones యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(1). (Accepted approach: Running count.)

Max Consecutive Ones లో సాధారణ పొరపాట్లు ఏమిటి?

Update the maximum on every step, not only on resets.