Skip to content

Commit 5d17ecf

Browse files
authored
Merge pull request #973 from pmjuu/main
[Lyla] Week 08
2 parents 23e315e + b122b9d commit 5d17ecf

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

โ€Žclone-graph/pmjuu.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(V + E)
3+
- ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋ฏ€๋กœ O(V)
4+
- ๊ฐ ๋…ธ๋“œ์˜ ๋ชจ๋“  ๊ฐ„์„ ์„ ํ•œ ๋ฒˆ์”ฉ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ O(E)
5+
- ๋”ฐ๋ผ์„œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(V + E)
6+
7+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(V + E)
8+
- ํด๋ก  ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ(clones): O(V)
9+
- BFS ํƒ์ƒ‰์„ ์œ„ํ•œ ํ(queue): O(V)
10+
- ๋ณต์ œ๋œ ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ์™€ ๊ฐ„์„  ์ €์žฅ ๊ณต๊ฐ„: O(V + E)
11+
'''
12+
13+
from typing import Optional
14+
from collections import deque
15+
# Definition for a Node.
16+
class Node:
17+
def __init__(self, val = 0, neighbors = None):
18+
self.val = val
19+
self.neighbors = neighbors if neighbors is not None else []
20+
21+
class Solution:
22+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
23+
if not node:
24+
return None
25+
26+
clones = { node.val: Node(node.val) }
27+
queue = deque([node])
28+
29+
while queue:
30+
current_node = queue.popleft()
31+
32+
for neighbor in current_node.neighbors:
33+
# add neighbors
34+
if neighbor.val not in clones.keys():
35+
queue.append(neighbor)
36+
clones[neighbor.val] = Node(neighbor.val)
37+
38+
clones[current_node.val].neighbors.append(clones[neighbor.val])
39+
40+
return clones[node.val]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(m * n)
3+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
4+
'''
5+
6+
class Solution:
7+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
8+
m, n = len(text1), len(text2)
9+
prev = [0] * (n + 1)
10+
11+
for i in range(1, m + 1):
12+
curr = [0] * (n + 1)
13+
for j in range(1, n + 1):
14+
if text1[i - 1] == text2[j - 1]:
15+
curr[j] = prev[j - 1] + 1
16+
else:
17+
curr[j] = max(prev[j], curr[j - 1])
18+
prev = curr # ํ˜„์žฌ ํ–‰์„ ์ด์ „ ํ–‰์œผ๋กœ ์—…๋ฐ์ดํŠธ
19+
20+
return prev[n]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
3+
- ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
4+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
5+
- char_count ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ตœ๋Œ€ ์•ŒํŒŒ๋ฒณ 26๊ฐœ๋งŒ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
6+
'''
7+
8+
class Solution:
9+
def characterReplacement(self, s: str, k: int) -> int:
10+
left = 0
11+
max_count = 0
12+
max_length = 0
13+
char_count = {}
14+
15+
for right in range(len(s)):
16+
char_count[s[right]] = char_count.get(s[right], 0) + 1
17+
max_count = max(max_count, char_count[s[right]])
18+
19+
# If the remaining characters exceed the allowed k changes
20+
while (right - left + 1) - max_count > k:
21+
char_count[s[left]] -= 1
22+
left += 1
23+
24+
max_length = max(max_length, right - left + 1)
25+
26+
return max_length

โ€Žnumber-of-1-bits/pmjuu.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„
3+
- format(n, 'b'): ์ •์ˆ˜๋ฅผ ์ด์ง„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์€ O(k)์ž…๋‹ˆ๋‹ค.
4+
- Counter(bits): ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž์˜ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉฐ, ์ด ์ž‘์—…๋„ ๋ฌธ์ž์—ด ๊ธธ์ด k์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค.
5+
- count['1']: ๋”•์…”๋„ˆ๋ฆฌ ์กฐํšŒ๋Š” ์ƒ์ˆ˜ ์‹œ๊ฐ„์ด๋ฏ€๋กœ O(1)์ž…๋‹ˆ๋‹ค.
6+
7+
์ด ์‹œ๊ฐ„ ๋ณต์žก๋„: O(k) + O(k) + O(1) = O(k)
8+
9+
๊ณต๊ฐ„ ๋ณต์žก๋„
10+
- format(n, 'b'): ์ƒ์„ฑ๋œ ์ด์ง„ ๋ฌธ์ž์—ด์€ ๊ธธ์ด k๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.
11+
- Counter(bits): ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ฐ ๋ฌธ์ž์˜ ๋นˆ๋„๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ตœ์•…์˜ ๊ฒฝ์šฐ, ๋‘ ๊ฐ€์ง€ ๋ฌธ์ž(โ€˜0โ€™๊ณผ โ€˜1โ€™)๋งŒ ์žˆ์œผ๋ฏ€๋กœ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(2) = O(1)๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
12+
13+
์ด ๊ณต๊ฐ„ ๋ณต์žก๋„: O(k)
14+
'''
15+
16+
from collections import Counter
17+
18+
class Solution:
19+
def hammingWeight(self, n: int) -> int:
20+
bits = format(n, 'b')
21+
count = Counter(bits)
22+
23+
return count['1']

โ€Žsum-of-two-integers/pmjuu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(1)
3+
- ๋ง์…ˆ์€ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜์—ฌ ์ˆ˜ํ–‰๋˜๋ฉฐ, ์ •์ˆ˜ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค.
4+
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
- ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
7+
'''
8+
9+
class Solution:
10+
def getSum(self, a: int, b: int) -> int:
11+
MASK = 0xFFFFFFFF # 32๋น„ํŠธ ์ •์ˆ˜ ๋งˆ์Šคํฌ
12+
MAX_INT = 0x7FFFFFFF # 32๋น„ํŠธ ์ •์ˆ˜์˜ ์ตœ๋Œ€๊ฐ’
13+
14+
while b:
15+
carry = (a & b) << 1 # ์ž๋ฆฌ ์˜ฌ๋ฆผ ๊ณ„์‚ฐ
16+
a = (a ^ b) & MASK # ๋ง์…ˆ ์ˆ˜ํ–‰
17+
b = carry & MASK # ์ž๋ฆฌ ์˜ฌ๋ฆผ๊ฐ’์„ ๋ฐ˜์˜ํ•˜์—ฌ ๋‹ค์Œ ์—ฐ์‚ฐ ์ง„ํ–‰
18+
19+
# ์Œ์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด 32๋น„ํŠธ ์ดˆ๊ณผ ์‹œ ๋ณด์ •
20+
return a if a <= MAX_INT else ~(a ^ MASK)

0 commit comments

Comments
ย (0)