Skip to content

Commit e731af4

Browse files
author
sangbeenmoon
committed
solved longest-consecutive-sequence.
1 parent eb3285e commit e731af4

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 30분 내로 풀지 못함. 풀이를 참고하였음.
2+
# idea : x - 1 이 존재하지 않는 x 값에서만 loop 를 실행한다.
3+
# time O(n) : 모든 숫자를 많아야 1,2번 방문.
4+
# space O(n)
5+
class Solution:
6+
def longestConsecutive(self, nums: List[int]) -> int:
7+
s = set(nums)
8+
answer = 0
9+
for num in s:
10+
if num - 1 not in s:
11+
cur = num - 1
12+
length = 0
13+
while cur + 1 in s:
14+
cur = cur + 1
15+
length = length + 1
16+
answer = max(answer , length)
17+
18+
return answer

0 commit comments

Comments
 (0)