problem 128, 2nd solution

This commit is contained in:
Yudhvir Singh
2024-06-16 17:40:14 -07:00
parent 2021d1a393
commit 5d3dc86ec7

View File

@@ -0,0 +1,22 @@
from typing import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# you make set
nums = set(nums)
longest = 0
for i in nums:
# check if n - 1 exists
if i - 1 not in nums:
temp_longest = 1
start = i
# now check if we have items in order in set
while start + 1 in nums:
temp_longest += 1
start += 1
longest = max(longest, temp_longest)
return longest