[리트코드][Medium] Longest Consecutive Sequence
문제
리트코드 Longest Consecutive Sequence
문제 풀이 및 회고
그냥 기록용.
내 풀이
from collections import defaultdict
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if nums == []:
return 0
nums.sort()
consecutive_elements = defaultdict(lambda: 0)
for i in range(len(nums)):
consecutive_elements[nums[i] + 1] = consecutive_elements[nums[i]] + 1
return max(consecutive_elements.values())
시간이 가장 짧았던 풀이
"""not mine obv"""
def longestConsecutive(t):
s = set(t)
q = 0
while s:
n = s.pop()
l = n - 1
while l in s:
s.remove(l)
l-=1
h = n + 1
while h in s:
s.remove(h)
h+=1
q = max(q, h-l-1)
return q
with open('user.out', 'w') as f:
for c in map(loads, stdin):
f.write(f"{longestConsecutive(c)}\n")
exit(0)
댓글남기기