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.