We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent eb3285e commit e731af4Copy full SHA for e731af4
1 file changed
longest-consecutive-sequence/sangbeenmoon.py
@@ -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