leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

0128-longest-consecutive-sequence.py

Open chasevanb opened this issue 2 years ago • 0 comments

        for n in nums:
            # check if its the start of a sequence
            if (n - 1) not in numSet:
                length = 1
                while (n + length) in numSet:
                    length += 1
                longest = max(length, longest)

If there are duplicates in nums, you repeat work. It's more efficient to do for n in numSet:

chasevanb avatar Apr 22 '23 23:04 chasevanb