Created
July 6, 2022 02:21
-
-
Save wanderindev/1248148380b1532be3aa27872e564ae9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def longest_consecutive(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
longest_sequence = 0 | |
set_of_nums = set(nums) | |
for num in nums: | |
if num - 1 not in set_of_nums: | |
sequence_length = 1 | |
while num + sequence_length in set_of_nums: | |
sequence_length += 1 | |
longest_sequence = max(longest_sequence, sequence_length) | |
return longest_sequence | |
# Test cases | |
sol = Solution() | |
nums = [100, 4, 200, 1, 3, 2] | |
assert sol.longest_consecutive(nums) == 4 | |
nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] | |
assert sol.longest_consecutive(nums) == 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment