Skip to content

Commit 8bc5cdd

Browse files
committed
feat: week1 - longest consecutive sequence
1 parent a380156 commit 8bc5cdd

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'''
2+
1μ°¨μ‹œλ„ : μ‹œκ°„ λ³΅μž‘λ„ O(n)으둜 ν•΄κ²° μ‹€νŒ¨..;;
3+
Approach
4+
- nums λ¦¬μŠ€νŠΈκ°€ λΉ„μ–΄μžˆλŠ” 경우 0을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
5+
- nums 리슀트의 쀑볡을 μ œκ±°ν•˜κ³  μ •λ ¬ν•©λ‹ˆλ‹€.
6+
- μ •λ ¬λœ 리슀트λ₯Ό μˆœνšŒν•˜λ©° 이전 μˆ«μžμ™€ ν˜„μž¬ μˆ«μžκ°€ μ—°μ†λ˜λŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.
7+
- μ—°μ†λœλ‹€λ©΄ countλ₯Ό μ¦κ°€μ‹œν‚€κ³ , μ—°μ†λ˜μ§€ μ•ŠλŠ”λ‹€λ©΄ max_count와 λΉ„κ΅ν•˜μ—¬ κ°±μ‹  ν›„ countλ₯Ό 1둜 μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.
8+
- μ΅œμ’…μ μœΌλ‘œ max_countλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
9+
10+
Time Complexity: O(n log n)
11+
- 쀑볡 제거 및 정렬에 O(n log n) λ°œμƒ
12+
13+
Space Complexity: O(n)
14+
- 쀑볡 제거된 리슀트 μ €μž₯ κ³΅κ°„μœΌλ‘œ 인해 O(n) λ°œμƒ
15+
'''
16+
class Solution:
17+
def longestConsecutive(self, nums: List[int]) -> int:
18+
# λ¦¬μŠ€νŠΈμ— 값이 μ—†λŠ” κ²½μš°λŠ” 0 λ°˜ν™˜
19+
if len(nums) == 0:
20+
return 0
21+
22+
# μ •λ ¬
23+
new_nums = list(set(nums))
24+
new_nums.sort()
25+
# λ³€μˆ˜ 생성 : λ°°μ—΄μ˜ 길이가 1이상인 경우 μ—°μ†λ˜λŠ” μˆ«μžλŠ” 무쑰건 1개 ν¬ν•¨λœλ‹€κ³  κ°€μ •
26+
count = 1
27+
max_count = 1
28+
29+
for idx, val in enumerate(new_nums):
30+
# 첫 번째 인덱트(idx == 0)λŠ” 비ꡐ할 이전 값이 μ—†μœΌλ―€λ‘œ κ·Έλƒ₯ κ±΄λ„ˆλ›°κΈ°
31+
if idx == 0:
32+
continue
33+
if new_nums[idx-1] +1 == val:
34+
count+=1
35+
# λ§ˆμ§€λ§‰ 값인 경우
36+
if idx == len(new_nums)-1:
37+
if max_count <= count:
38+
max_count = count
39+
else:
40+
# 이전갑과 μ—°μ†λ˜μ§€ μ•ŠλŠ” 경우
41+
if max_count <= count:
42+
max_count = count
43+
count = 1
44+
else:
45+
count = 1
46+
47+
return max_count
48+
49+
'''
50+
2μ°¨μ‹œλ„ : Set 자료ꡬ쑰 ν™œμš©
51+
Approach
52+
- Set 자료ꡬ쑰둜 쀑볡 값을 μ œκ±°ν•˜κ³ , Set을 ν™œμš©ν•œ 쑴재 μ—¬λΆ€ 확인 μ‹œκ°„μ€ O(1)이기 λ•Œλ¬Έμ— 이λ₯Ό ν™œμš©ν–ˆμŠ΅λ‹ˆλ‹€.
53+
- nums λ¦¬μŠ€νŠΈκ°€ λΉ„μ–΄μ΄λŠ” 경우 0을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
54+
- Set으둜 μ€‘λ³΅λœ 값을 μ œκ±°ν•©λ‹ˆλ‹€.
55+
- Set을 μˆœνšŒν•˜λ©° 각 값에 λŒ€ν•΄ val-1이 Set에 μ—†λŠ” 경우 μƒˆλ‘œμš΄ 연속 μˆ˜μ—΄μ˜ μ‹œμž‘μ μœΌλ‘œ κ°„μ£Όν•©λ‹ˆλ‹€.
56+
- μ‹œμž‘μ  μ΄ν›„λ‘œ While문을 μ‚¬μš©ν•΄μ„œ val+1, val+2,... κ°€ Set에 μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜λ©° countλ₯Ό μ¦κ°€μ‹œν‚΅λ‹ˆλ‹€.
57+
- μ΅œμ’…μ μœΌλ‘œ max_countλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
58+
59+
Time Complexity: O(n)
60+
- Arrayλ₯Ό Set으둜 λ³€ν™˜: O(n)
61+
- 각 μ›μ†Œμ— λŒ€ν•΄:
62+
- μ‹œμž‘μ μ΄ μ•„λ‹Œ 경우: if (val - 1) not in num_set β†’ O(1) ν›„ λ°”λ‘œ λ„˜μ–΄κ°
63+
- μ‹œμž‘μ μΈ 경우: while둜 연속 ꡬ간을 λκΉŒμ§€ 탐색: 각 μˆ«μžλŠ” μ—°μ†λ˜λŠ” 숫자 λ‚΄μ—μ„œ μ΅œλŒ€ ν•œ λ²ˆμ”©λ§Œ λ°©λ¬Έλ˜λ―€λ‘œ,
64+
전체 while 반볡 횟수의 합은 O(n)을 λ„˜μ§€ μ•ŠμŒ.
65+
66+
Space Complexity: O(n)
67+
- Set μžλ£Œκ΅¬μ‘°μ— 쀑볡 제거된 값듀을 μ €μž₯ν•˜λŠ”λ° O(n) λ°œμƒ
68+
'''
69+
class Solution:
70+
def longestConsecutive(self, nums: List[int]) -> int:
71+
# 값이 μ—†λŠ” 경우 0 λ°˜ν™˜
72+
if len(nums) == 0:
73+
return 0
74+
75+
# set으둜 μ€‘λ³΅λœ κ°’ 제거
76+
num_set = set(nums)
77+
max_count = 1
78+
79+
for val in num_set:
80+
# val-1 이 μ—†λ‹€λ©΄, val은 μƒˆλ‘œμš΄ 연속 μˆ˜μ—΄μ˜ μ‹œμž‘μ 
81+
if (val - 1) not in num_set:
82+
current = val
83+
count = 1
84+
85+
# μ‹œμž‘μ  이후 μ—°μ†λ˜λŠ” μˆ«μžκ°€ setμ•ˆμ— μžˆλŠ”μ§€ 검증
86+
while (current + 1) in num_set:
87+
current += 1
88+
count += 1
89+
# whileλ¬Έ νƒˆμΆœ (=μ—°μ†λ˜λŠ” μˆ«μžκ°€ μ—†λŠ” 경우) μ΅œλŒ€ μ—°μ†λœ 숫자 길이와 비ꡐ함
90+
if count > max_count:
91+
max_count = count
92+
93+
return max_count

0 commit comments

Comments
Β (0)