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.