Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 485 · Linear Scan

Max Consecutive Ones

LeetCode 485 · सोपा · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Running streak, reset on non-1.

सोपाArray

मी 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.