Array Array

Longest Consecutive

~2 mins read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def longestConsecutive(nums: List[int]) -> int:
    """
    for each num:
        if num-1 in set, continue
        check the streak 
    """
    numset = set(nums)
    ans = 0

    def streak(num):
        streak = 1

        while num + 1 in numset:
            streak += 1
            num += 1

        return streak

    for num in nums:
        if num - 1 in numset:
            continue
        ans = max(ans, streak(num))

    return ans


assert longestConsecutive([100, 4, 200, 1, 3, 2]) == 4  # 1,2,3,4

🎰