Skip to content

Commit 2e36ae9

Browse files
committed
add solution: longest consecutive sequence
1 parent d9c84e7 commit 2e36ae9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
๋ฌธ์ œ: ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ๊ธด ์—ฐ์†๋œ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ
3+
ํ’€์ด: ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ํ›„, ์—ฐ์†๋œ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐ
4+
(์ค‘๋ณต๋œ ๊ฐ’์€ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ œ๊ฑฐ ํ›„ ์ฒ˜๋ฆฌ)
5+
6+
์‹œ๊ฐ„๋ณต์žก๋„: O(k log k + N) (k๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค ๊ฐœ์ˆ˜, N์€ nums์˜ ๊ธธ์ด)
7+
๊ณต๊ฐ„๋ณต์žก๋„: O(N) (๊ณต๊ฐ„์˜ ํฌ๊ธฐ๋Š” nums์˜ ๊ธธ์ด์— ๋น„๋ก€)
8+
9+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋”•์…”๋„ˆ๋ฆฌ, ๋ฆฌ์ŠคํŠธ
10+
'''
11+
12+
13+
class Solution:
14+
def longestConsecutive(self, nums: List[int]) -> int:
15+
d = {}
16+
answ = 1
17+
if len(nums) > 0:
18+
for i in nums:
19+
if i not in d:
20+
d[i] = 0
21+
22+
a = sorted(d.keys())
23+
now = a[0]
24+
an = 1
25+
for i in range(1, len(a)):
26+
t = a[i]
27+
if now+1 == t:
28+
an += 1
29+
else:
30+
an = 1
31+
now = a[i]
32+
answ = max(answ, an)
33+
34+
return answ
35+
return 0
36+
37+

0 commit comments

Comments
ย (0)