Problem :

https://leetcode.com/problems/longest-consecutive-sequence/


My Solution :

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = set(nums)
ans = 0
for n in nums:
if n-1 not in nums:
m = n
while m in nums:
m += 1
ans = max(ans, m-n)
return ans


Comment :

개인적으로 set과 dictionary를 참 좋아한다. 탐색 시간이 O(1)이기 때문이다.