Skip to content

Commit 9b7b419

Browse files
Merge pull request #2000 from Seoya0512/main
[Seoya0512] WEEK1 solutions
2 parents 121cf70 + 0fd9c87 commit 9b7b419

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Approach:
3+
μ£Όμ–΄μ§„ λ°°μ—΄μ—μ„œ μ€‘λ³΅λœ μš”μ†Œκ°€ μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
4+
ν•΄μ‹œ ν…Œμ΄λΈ” 자료ꡬ쑰인 set을 μ΄μš©ν•΄ nums의 λͺ¨λ“  μš”μ†Œλ₯Ό μˆœνšŒν•˜λ©° 쀑볡을 μ œκ±°ν•œ 객체λ₯Ό μƒμ„±ν–ˆμŠ΅λ‹ˆλ‹€.
5+
이후 set의 길이와 원본 λ°°μ—΄μ˜ 길이λ₯Ό λΉ„κ΅ν•˜μ—¬, 길이가 λ‹€λ₯΄λ‹€λ©΄ μ€‘λ³΅λœ μš”μ†Œκ°€ μ‘΄μž¬ν•¨μ„ μ˜λ―Έν•©λ‹ˆλ‹€.
6+
7+
Time Complexity:
8+
O(n)
9+
- set(nums)λ₯Ό 생성할 λ•Œ nums의 λͺ¨λ“  μ›μ†Œλ₯Ό ν•œ λ²ˆμ”© μˆœνšŒν•˜λ©° ν•΄μ‹œ ν…Œμ΄λΈ”μ— μ‚½μž…ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
10+
11+
Space Complexity:
12+
O(n)
13+
- 쀑볡을 μ œκ±°ν•œ μ›μ†Œλ“€μ΄ μƒˆλ‘œμš΄ μ§‘ν•©(set)에 μ €μž₯λ˜λ―€λ‘œ,
14+
μ΅œλŒ€ nums의 크기만큼 μΆ”κ°€ 곡간이 ν•„μš”ν•©λ‹ˆλ‹€.
15+
'''
16+
class Solution:
17+
def containsDuplicate(self, nums: List[int]) -> bool:
18+
if len(nums) != len(set(nums)):
19+
return True
20+
else:
21+
return False
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
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from typing import List
2+
3+
'''
4+
[1μ°¨ μ‹œλ„]
5+
Approach: nums 리슀트의 각 μ›μ†Œμ˜ λΉˆλ„μˆ˜λ₯Ό hash_map에 μ €μž₯ν›„, λΉˆλ„μˆ˜ κΈ°μ€€μœΌλ‘œ 정렬을 ν•˜κ³  μƒμœ„ k개λ₯Ό λ°˜ν™˜
6+
count ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ„œ κ°„κ²¬ν• κ²Œ μ½”λ“œλ₯Ό μ™„μ„±ν•˜κ³ μž ν–ˆμŠ΅λ‹ˆλ‹€.
7+
8+
Time Complexity: O(nΒ²)
9+
- for num in nums μˆœν™˜κ³Ό nums.count(num) 의 μ€‘μ²©μœΌλ‘œ 인해 O(nΒ²) λ°œμƒ
10+
11+
Space Complexity: O(n)
12+
- hash_map μ €μž₯ 곡간과 sorted_hash_map μ €μž₯ κ³΅κ°„μœΌλ‘œ 인해 O(n) λ°œμƒ
13+
14+
-
15+
'''
16+
class Solution:
17+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
18+
hash_map = {}
19+
for num in nums:
20+
hash_map[num] = nums.count(num)
21+
22+
# hash_map μ •λ ¬
23+
sorted_hash_map = sorted(hash_map.items(), key=lambda x: x[1], reverse=True)
24+
# μƒμœ„ k개 λ°˜ν™˜
25+
return [x[0] for x in sorted_hash_map[:k]]
26+
27+
'''
28+
[2μ°¨ μ‹œλ„]
29+
Approach: μœ„μ˜ λ°©μ‹μ—μ„œ count ν•¨μˆ˜λ₯Ό μ œκ±°ν•˜κ³ , hash_map에 λΉˆλ„μˆ˜λ₯Ό 직접 μΉ΄μš΄νŒ… ν•˜λ„λ‘ μˆ˜μ •ν•˜λ„λ‘ ν•΄μ„œ
30+
Time Complexityλ₯Ό κ°œμ„ ν•˜κΈ° μœ„ν•΄ λ…Έλ ₯ν–ˆμŠ΅λ‹ˆλ‹€.
31+
32+
Time Complexity: O(n log n)
33+
- for num in nums μˆœν™˜μ— O(n) λ°œμƒ
34+
- hash_map 정렬에 O(m log m)으둜 예츑 되며 μ΅œλŒ€ m =< n μ΄λ―€λ‘œ O(n log n) λ°œμƒ
35+
36+
'''
37+
class Solution:
38+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
39+
hash_map = {}
40+
for num in nums:
41+
if hash_map.get(num):
42+
hash_map.update({num: hash_map[num]+1})
43+
else:
44+
hash_map.update({num: 1})
45+
46+
# hash_map μ •λ ¬
47+
sorted_hash_map = sorted(hash_map.items(), key=lambda x: x[1], reverse=True)
48+
# μƒμœ„ k개 λ°˜ν™˜
49+
return [x[0] for x in sorted_hash_map[:k]]
50+
51+
52+
'''
53+
[3μ°¨ μ‹œλ„ - Bucket Sort ν™œμš©]
54+
Approach: Bucket Sort 방식을 ν™œμš©ν•  수 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λμŠ΅λ‹ˆλ‹€.
55+
for loop와 쑰건문이 λ°˜λ³΅λ˜κΈ°λ„ ν•˜μ§€λ§Œ, λ°°μ—΄μ˜ 크기에 맞좰 ν•œλ²ˆμ”©λ§Œ μˆœνšŒν•˜κΈ° λ•Œλ¬Έμ—
56+
Time Complexityλ₯Ό O(n)으둜 κ°œμ„ ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
57+
58+
Time Complexity: O(n)
59+
- for num in nums μˆœν™˜ν•˜λ©° λΉˆλ„μˆ˜ μΉ΄μš΄νŒ…: O(n)
60+
- λΉˆλ„μ— 따라 μˆ«μžλ“€μ„ 버킷에 λ„£κΈ°: O(n)
61+
- 버킷을 λ’€μ—μ„œλΆ€ν„° μˆœνšŒν•˜λ©° μƒμœ„ k개 숫자 μ±„μš°κΈ°: O(n)
62+
63+
Space Complexity: O(n)
64+
- hash_map λ”•μ…”λ„ˆλ¦¬μ™€ bucket 리슀트둜 인해 O(n) λ°œμƒ
65+
'''
66+
class Solution:
67+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
68+
hash_map = {}
69+
for num in nums:
70+
hash_map[num] = hash_map.get(num, 0) + 1
71+
72+
# μ˜ˆμ‹œ nums = [1,6,6,3,2,6,8], bucket = [[1,3,2,8],[],[6]]
73+
bucket = [[] for _ in range(len(nums) + 1)]
74+
for num, count in hash_map.items():
75+
bucket[count].append(num)
76+
77+
result: List[int] = []
78+
# κ°€μž₯ 큰 λΉˆλ„λΆ€ν„° λ‚΄λ €μ˜€λ©΄μ„œ 숫자 μˆ˜μ§‘ (for 루프 μ—­μˆœ)
79+
for count in range(len(bucket) - 1, -1, -1):
80+
if not bucket[count]:
81+
continue
82+
83+
for num in bucket[count]:
84+
result.append(num)
85+
if len(result) == k:
86+
return result
87+
88+
return result

β€Žtwo-sum/Seoya0512.pyβ€Ž

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Approach:
3+
μ£Όμ–΄μ§„ 배열을 두 번 μˆœνšŒν•˜λ©° λͺ¨λ“  κ°€λŠ₯ν•œ μŒμ„ κ²€μ‚¬ν•©λ‹ˆλ‹€.
4+
각 μ›μ†Œμ— λŒ€ν•΄ μ΄ν›„μ˜ μ›μ†Œλ“€κ³Όμ˜ 합이 targetκ³Ό 같은지 λΉ„κ΅ν•˜μ—¬ μΌμΉ˜ν•˜λŠ” 경우 인덱슀λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
5+
6+
Time Complexity:
7+
O(nΒ²)
8+
- Outer Loopμ—μ„œ n번 반볡
9+
- Inner Loopμ—μ„œ 각 idxλ§ˆλ‹€ μ΅œμ†Œ n-1번 반볡
10+
11+
Space Complexity:
12+
O(1)
13+
- κ²°κ³Όλ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•΄ μƒμˆ˜ 값을 ν•„μš”λ‘œ ν•˜μ§€λ§Œ, μž…λ ₯κ°’μ˜ 크킀에 λΉ„λ‘€ν•œ μƒˆλ‘œμš΄ 곡간 λΆˆμš”
14+
'''
15+
16+
class Solution:
17+
def twoSum(self, nums: List[int], target: int) -> List[int]:
18+
for idx, xnum in enumerate(nums):
19+
for jdx in range(idx + 1, len(nums)):
20+
if (xnum + nums[jdx]) == target:
21+
return [idx, jdx]
22+
return []
23+
24+
'''
25+
(κ°œμ„ )
26+
Approach:
27+
β€œYou may assume that each input would have exactly one solution, and you may not use the same element twice.”
28+
λΌλŠ” λ¬Έκ΅¬μ—μ„œ 항상 정닡이 μ‘΄μž¬ν•˜λ©° 같은 μ›μ†Œλ₯Ό 쀑볡 μ‚¬μš©ν•  수 μ—†μŒμ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.
29+
이λ₯Ό λ°”νƒ•μœΌλ‘œ 각 μ›μ†Œ num에 λŒ€ν•΄ (target - num)이 이전에 λ“±μž₯ν•œ 적이 μžˆλŠ”μ§€λ₯Ό ν•΄μ‹œλ§΅μ„ μ΄μš©ν•΄ λΉ λ₯΄κ²Œ ν™•μΈν•˜λ„λ‘ κ°œμ„ ν–ˆμŠ΅λ‹ˆλ‹€.
30+
즉, 쀑볡이 μ—†κ³  닡이 λ°˜λ“œμ‹œ μ‘΄μž¬ν•œλ‹€λŠ” 쑰건 μ•„λž˜μ—μ„œ, inner loop 없이 λ°”λ‘œ μ°ΎκΈ° μœ„ν•΄ λ”•μ…”λ„ˆλ¦¬λ₯Ό μ‚¬μš©ν•˜ κ²ƒμž…λ‹ˆλ‹€.
31+
32+
Time Complexity:
33+
O(n)
34+
- 리슀트λ₯Ό ν•œ 번 μˆœνšŒν•˜λ©΄μ„œ λ”•μ…”λ„ˆλ¦¬λ₯Ό μ±„μ›Œ λ„£λŠ” 데 κ±Έλ¦¬λŠ” 전체 μ‹œκ°„
35+
36+
Space Complexity:
37+
O(n)
38+
- ν•΄μ‹œλ§΅μ— key-value 쌍으둜 μ €μž₯λ˜λŠ” 곡간
39+
'''
40+
class Solution:
41+
def twoSum(self, nums: List[int], target: int) -> List[int]:
42+
hash_map = {}
43+
for i, num in enumerate(nums):
44+
diff = target - num
45+
if diff in hash_map:
46+
return [hash_map[diff], i]
47+
hash_map[num] = i

0 commit comments

Comments
Β (0)