Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 128 · Hashing / Anagram

Longest Consecutive Sequence

LeetCode 128 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Only expand from run starts (num − 1 not in set).

நடுத்தரம்ArrayHash TableUnion-Find
LeetCode இல் திற ↗

நான் Longest Consecutive Sequence (LeetCode 128) ஐ இப்படித் தீர்த்தேன் — Hashing / Anagram பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Put everything in a set. A number only starts a run if num − 1 isn't present; from each such start, count upward while the next number exists. Each number is visited by at most one run, so the whole thing is linear.

Approach

  1. 1

    Build a set (removes duplicates, O(1) lookups).

  2. 2

    For each num with no predecessor, walk num+1, num+2, … counting length.

  3. 3

    Track the longest run.

நான் எப்படித் தீர்த்தேன்

Hash set + run startsPythonO(n) time · O(n) space
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        if not nums:
            return 0
        unique_elenents = set(nums) #remove duplicates
        max_length = 1 #single element 
        for num in unique_elenents:
            if num-1 not in unique_elenents:
                length = 1
                while num+1 in unique_elenents:
                    num = num+1
                    length+=1
                max_length=max(max_length,length)
        return max_length
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Longest Consecutive Sequence — அடிக்கடி கேட்கப்படும் கேள்விகள்

Longest Consecutive Sequence பின்னாலுள்ள intuition என்ன?

Put everything in a set. A number only starts a run if num − 1 isn't present; from each such start, count upward while the next number exists. Each number is visited by at most one run, so the whole thing is linear.

Longest Consecutive Sequence (LeetCode 128) ஐ எப்படித் தீர்ப்பது?

1. Build a set (removes duplicates, O(1) lookups). 2. For each num with no predecessor, walk num+1, num+2, … counting length. 3. Track the longest run.

Longest Consecutive Sequence இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(n). (Accepted approach: Hash set + run starts.)

Longest Consecutive Sequence இல் பொதுவான தவறுகள் என்ன?

Without the 'is it a start?' check the algorithm degrades to O(n²). Empty input returns 0.