Skip to content

Commit

Permalink
Add longest consecutive sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyannn committed Jan 18, 2024
1 parent d842f70 commit 3efe906
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lc_0128_longest_consequtive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""128. Longest Consecutive Sequence
Runtime: 334ms
Beats 96.86% of users with Python3
"""
import unittest
from itertools import count
from typing import List


class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
els = set(nums)

def gen():
for n in els:
if n - 1 not in els and n + 1 in els:
for k in count(2):
if n + k not in els:
yield k
break

return max(gen(), default=1) if nums else 0


class LongestConsecutiveTests(unittest.TestCase):
def setUp(self):
self.sol = Solution()

def test_longestConsecutive(self):
self.assertEqual(self.sol.longestConsecutive([]), 0)
self.assertEqual(self.sol.longestConsecutive([100, 4, 200, 1, 3, 2]), 4)
self.assertEqual(self.sol.longestConsecutive([1, 2, 0, 1]), 3)
self.assertEqual(self.sol.longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]), 9)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3efe906

Please sign in to comment.