Skip to content

Commit d23f613

Browse files
authored
Merge pull request #958 from KwonNayeon/main
[KwonNayeon] Week 8
2 parents efca6c2 + 7af62fc commit d23f613

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

โ€Žclone-graph/KwonNayeon.pyโ€Ž

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Constraints:
3+
- The number of nodes in the graph is in the range [0, 100].
4+
- 1 <= Node.val <= 100
5+
- Node.val is unique for each node.
6+
- There are no repeated edges and no self-loops in the graph.
7+
- The Graph is connected and all nodes can be visited starting from the given node.
8+
9+
Shallow Copy (์–•์€ ๋ณต์‚ฌ):
10+
- ๋…ธ๋“œ ์ž์ฒด๋Š” ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ์— ๋ณต์‚ฌ
11+
- ํ•˜์ง€๋งŒ neighbors๋Š” ์›๋ณธ ๋…ธ๋“œ์˜ neighbors๋ฅผ ๊ทธ๋Œ€๋กœ ์ฐธ์กฐ
12+
์˜ˆ์‹œ) ์›๋ณธ Node1์ด Node2๋ฅผ neighbor๋กœ ๊ฐ€์งˆ ๋•Œ
13+
๋ณต์‚ฌํ•œ CopyNode1์€ ์ƒˆ๋กœ์šด ๋…ธ๋“œ์ง€๋งŒ
14+
CopyNode1์˜ neighbor๋Š” ์›๋ณธ์˜ Node2๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€๋ฆฌํ‚ด
15+
16+
Deep Copy (๊นŠ์€ ๋ณต์‚ฌ):
17+
- ๋…ธ๋“œ๋Š” ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ์— ๋ณต์‚ฌ
18+
- neighbors๋„ ๋ชจ๋‘ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋กœ ๋ณต์‚ฌํ•ด์„œ ์—ฐ๊ฒฐ
19+
์˜ˆ์‹œ) ์›๋ณธ Node1์ด Node2๋ฅผ neighbor๋กœ ๊ฐ€์งˆ ๋•Œ
20+
CopyNode1๋„ ์ƒˆ๋กœ์šด ๋…ธ๋“œ์ด๊ณ 
21+
CopyNode1์˜ neighbor๋„ ์ƒˆ๋กœ ๋งŒ๋“  CopyNode2๋ฅผ ๊ฐ€๋ฆฌํ‚ด
22+
23+
Time Complexity: O(N + E)
24+
- N: ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
25+
- E: ์—ฃ์ง€์˜ ๊ฐœ์ˆ˜
26+
- ๋ชจ๋“  ๋…ธ๋“œ์™€ ์—ฃ์ง€๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ
27+
28+
Space Complexity: O(N)
29+
- N: ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
30+
- dictionary์™€ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ ๊ณต๊ฐ„
31+
32+
# Definition for a Node.
33+
class Node:
34+
def __init__(self, val = 0, neighbors = None):
35+
self.val = val
36+
self.neighbors = neighbors if neighbors is not None else []
37+
38+
์ฐธ๊ณ  ์‚ฌํ•ญ:
39+
- ํ˜ผ์ž ํ’€๊ธฐ ์–ด๋ ค์›Œ์„œ, ๋ฌธ์ œ์™€ ๋‹ต์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์— ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค!
40+
"""
41+
class Solution:
42+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
43+
if not node:
44+
return None
45+
46+
dict = {}
47+
48+
def dfs(node):
49+
if node.val in dict: # ์ด๋ฏธ ๋ณต์‚ฌํ•œ ๋…ธ๋“œ๋ผ๋ฉด
50+
return dict[node.val] # ํ•ด๋‹น ๋ณต์‚ฌ๋ณธ ๋ฐ˜ํ™˜
51+
52+
# ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ƒ์„ฑ
53+
copy = Node(node.val)
54+
dict[node.val] = copy # dictionary์— ๊ธฐ๋ก
55+
56+
# ๊ฐ neighbor์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ™์€ ๊ณผ์ • ์ˆ˜ํ–‰
57+
for neighbor in node.neighbors:
58+
copy.neighbors.append(dfs(neighbor))
59+
60+
return copy
61+
62+
return dfs(node)
63+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Constraints:
3+
- 1 <= text1.length, text2.length <= 1000
4+
- text1 and text2 consist of only lowercase English characters.
5+
6+
Time Complexity: O(m*n)
7+
- m์€ text1์˜ ๊ธธ์ด, n์€ text2์˜ ๊ธธ์ด
8+
- @cache๋กœ ์ค‘๋ณต ๊ณ„์‚ฐ์„ ๋ฐฉ์ง€ํ•˜์—ฌ ๊ฐ (i,j) ์กฐํ•ฉ์„ ํ•œ ๋ฒˆ๋งŒ ๊ณ„์‚ฐํ•จ
9+
10+
Space Complexity: O(m*n)
11+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ˜ธ์ถœ ์Šคํƒ์ด ๋‘ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ๊ณฑ๋งŒํผ ๊นŠ์–ด์ง
12+
13+
ํ’€์ด๋ฐฉ๋ฒ•:
14+
1. DFS์™€ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์‚ฌ์šฉ
15+
2. ๊ฐ ์œ„์น˜ (i,j)์—์„œ:
16+
- ๋ฌธ์ž๊ฐ€ ๊ฐ™์œผ๋ฉด: ํ˜„์žฌ ๋ฌธ์ž๋ฅผ ํฌํ•จ(+1)ํ•˜๊ณ  ์–‘์ชฝ ๋‹ค์Œ์œผ๋กœ ์ด๋™
17+
- ๋‹ค๋ฅด๋ฉด: ํ•œ์ชฝ๋งŒ ์ด๋™ํ•œ ๊ฒฝ์šฐ ์ค‘ ์ตœ๋Œ“๊ฐ’ ์„ ํƒ
18+
3. base case: ์–ด๋А ํ•œ์ชฝ ๋ฌธ์ž์—ด ๋์— ๋„๋‹ฌํ•˜๋ฉด ์ข…๋ฃŒ
19+
"""
20+
21+
from functools import cache
22+
class Solution:
23+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
24+
@cache
25+
def dfs(i, j):
26+
if i == len(text1) or j == len(text2):
27+
return 0
28+
29+
if text1[i] == text2[j]:
30+
return 1 + dfs(i + 1, j + 1)
31+
32+
return max(dfs(i + 1, j), dfs(i, j + 1))
33+
34+
return dfs(0, 0)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Constraints:
3+
- 1 <= s.length <= 10^5
4+
- s consists of only uppercase English letters.
5+
- 0 <= k <= s.length
6+
7+
Time Complexity: O(n)
8+
- ์—ฌ๊ธฐ์„œ n์€ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
9+
10+
Space Complexity: O(1)
11+
- ์ถ”๊ฐ€ ๋ณ€์ˆ˜(left, right, max_length ๋“ฑ)๋Š” ์ƒ์ˆ˜ ๊ฐœ
12+
13+
ํ’€์ด๋ฐฉ๋ฒ•:
14+
1. Sliding Window๋กœ ๊ตฌ๊ฐ„์„ ๊ด€๋ฆฌ
15+
- right ํฌ์ธํ„ฐ๋กœ ๊ตฌ๊ฐ„์„ ๋Š˜๋ฆฌ๋‹ค๊ฐ€
16+
- ๋ณ€๊ฒฝํ•ด์•ผํ•˜๋Š” ๋ฌธ์ž ์ˆ˜๊ฐ€ k๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด left ํฌ์ธํ„ฐ๋กœ ๊ตฌ๊ฐ„์„ ์ค„์ž„
17+
18+
2. ๊ฐ ๊ตฌ๊ฐ„์—์„œ:
19+
- ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๋ฌธ์ž๋กœ ๋‚˜๋จธ์ง€๋ฅผ ๋ณ€๊ฒฝ
20+
- (๊ตฌ๊ฐ„ ๊ธธ์ด - ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๋ฌธ์ž ์ˆ˜)๊ฐ€ k ์ดํ•˜์—ฌ์•ผ ํ•จ
21+
"""
22+
class Solution:
23+
def characterReplacement(self, s: str, k: int) -> int:
24+
counter = {}
25+
left = 0
26+
max_length = 0
27+
28+
for right in range(len(s)):
29+
counter[s[right]] = counter.get(s[right], 0) + 1
30+
31+
curr_length = right - left + 1
32+
33+
if curr_length - max(counter.values()) > k:
34+
counter[s[left]] -= 1
35+
left += 1
36+
37+
max_length = max(max_length, right - left + 1)
38+
39+
return max_length
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Constraints:
3+
- 1 <= n <= 2^31 - 1
4+
5+
Time Complexity: O(k)
6+
- ์—ฌ๊ธฐ์„œ k๋Š” ์ž…๋ ฅ์˜ ๋น„ํŠธ ์ˆ˜
7+
- ์ด ๊ฒฝ์šฐ 32๋น„ํŠธ ์ •์ˆ˜์ด๋ฏ€๋กœ ์‹ค์งˆ์ ์œผ๋กœ O(1) (ํ•ญ์ƒ ์ตœ๋Œ€ 32๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ)
8+
9+
Space Complexity: O(1)
10+
- count ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ๊ณต๊ฐ„ ๋ณต์žก๋„
11+
"""
12+
class Solution:
13+
def hammingWeight(self, n: int) -> int:
14+
count = 0
15+
while n:
16+
count += n & 1 # ํ˜„์žฌ ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๊ฐ€ 1์ธ์ง€ ํ™•์ธ
17+
n >>= 1 # ๋‹ค์Œ ๋น„ํŠธ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•ด ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธ
18+
return count
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Constraints:
3+
- -1000 <= a, b <= 1000
4+
5+
Time Complexity: O(1)
6+
7+
Space Complexity: O(1)
8+
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ž…๋ ฅ๋ฐ›์€ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
9+
10+
ํ’€์ด๋ฐฉ๋ฒ•:
11+
1. XOR(^)์—ฐ์‚ฐ์„ ํ†ตํ•ด ์บ๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๊ฐ ์ž๋ฆฌ์˜ ํ•ฉ์„ ๊ตฌํ•จ
12+
2. AND(&)์—ฐ์‚ฐ ํ›„ ์™ผ์ชฝ ์‹œํ”„ํŠธ(<<)๋กœ ๋‹ค์Œ ์ž๋ฆฌ๋กœ ์˜ฌ๋ผ๊ฐˆ ์บ๋ฆฌ๋ฅผ ๊ตฌํ•จ
13+
3. ์บ๋ฆฌ๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ 1-2 ๊ณผ์ •์„ ๋ฐ˜๋ณต
14+
"""
15+
# Solution 1: ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ๋ฒ„์ „
16+
class Solution:
17+
def getSum(self, a: int, b: int) -> int:
18+
while b:
19+
current_sum = a ^ b
20+
21+
next_carry = (a & b) << 1
22+
23+
a = current_sum
24+
b = next_carry
25+
26+
return a
27+
28+
# Solution 2: ์ตœ์ ํ™” ๋ฒ„์ „
29+
class Solution:
30+
def getSum(self, a: int, b: int) -> int:
31+
while b:
32+
a, b = a ^ b, (a & b) << 1
33+
34+
return a
35+

0 commit comments

Comments
ย (0)