Skip to content

Latest commit

 

History

History

128-LongestConsecutiveSequence

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Longest Consecutive Sequence

Problem can be found in here!

Solution1: Disjoint Set

Time Complexity: O(n), Space Complexity: O(n)

Solution2: Hash Table

The solution is from leetcode.

def longestConsecutive(nums: List[int]) -> int:
    longest_sequence = 0
    number_set = set(nums)

    for num in number_set:
        if num-1 not in number_set:
            current_num = num
            current_streak = 1

            while current_num+1 in number_set:
                current_num += 1
                current_streak += 1

            longest_sequence = max(longest_sequence, current_streak)

    return longest_sequence

Time Complexity: O(n), Space Complexity: O(n)