| ps | |
|---|---|
| 링크 | leetcode.com/… |
| 출처 | LeetCode |
| 문제 번호 | 128 |
| 문제명 | Longest Consecutive Sequence |
| 레벨 | Medium |
| 분류 |
기본 |
| 시간복잡도 | O(n) |
| 인풋사이즈 | n<=10^5 |
| 사용한 언어 | python 3.14 |
| 제출기록 | 36.68MB / 53ms |
| 최고기록 | 0ms |
| 해결날짜 | 2026/05/16 |
"""Solution code for "LeetCode 128. Longest Consecutive Sequence".
- Problem link: https://leetcode.com/problems/longest-consecutive-sequence
- Solution link: http://www.teferi.net/ps/problems/leetcode/128
"""
from typing import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0
inf = max(nums) + 2
num_set = set(nums)
answer = 0
for x in num_set:
if x - 1 in num_set:
continue
length = next(i for i in range(x, inf) if i not in num_set) - x
answer = max(answer, length)
return answer"""Solution code for "LeetCode 128. Longest Consecutive Sequence".
- Problem link: https://leetcode.com/problems/longest-consecutive-sequence
- Solution link: http://www.teferi.net/ps/problems/leetcode/128
"""
import collections
from typing import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
return max(
collections.Counter(
x - i for i, x in enumerate(sorted(set(nums)))
).values(),
default=0,
)