Skip to content

Commit 65a41cd

Browse files
authored
Merge pull request #1900 from hu6r1s/main
[hu6r1s] WEEK 08 Solutions
2 parents 56ce555 + 9c22f9e commit 65a41cd

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

clone-graph/hu6r1s.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val = 0, neighbors = None):
5+
self.val = val
6+
self.neighbors = neighbors if neighbors is not None else []
7+
"""
8+
9+
from typing import Optional
10+
class Solution:
11+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
12+
if not node:
13+
return None
14+
15+
visited = [None] * 101
16+
17+
def dfs(n):
18+
if visited[n.val] is not None:
19+
return visited[n.val]
20+
21+
copy = Node(n.val, [])
22+
visited[n.val] = copy
23+
24+
for nei in n.neighbors:
25+
copy.neighbors.append(dfs(nei))
26+
return copy
27+
28+
return dfs(node)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
"""
3+
문제의 힌트에서 DP를 활용하는 것을 확인
4+
"""
5+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
6+
dp = [[0] * (len(text2)+1) for _ in range(len(text1)+1)]
7+
8+
for i in range(1, len(text1)+1):
9+
for j in range(1, len(text2)+1):
10+
if text1[i-1] == text2[j-1]:
11+
dp[i][j] = dp[i-1][j-1] + 1
12+
else:
13+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
14+
15+
return dp[-1][-1]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def characterReplacement(self, s: str, k: int) -> int:
3+
counts = {}
4+
left = 0
5+
max_count = 0
6+
res = 0
7+
8+
for right in range(len(s)):
9+
counts[s[right]] = counts.get(s[right], 0) + 1
10+
max_count = max(max_count, counts[s[right]])
11+
12+
while (right - left + 1) - max_count > k:
13+
counts[s[left]] -= 1
14+
left += 1
15+
16+
res = max(res, right - left + 1)
17+
18+
return res

palindromic-substrings/hu6r1s.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
"""
3+
브루트포스로 모두 탐색하면서 집어넣는 방법
4+
O(n^2)
5+
"""
6+
def countSubstrings(self, s: str) -> int:
7+
cnt = []
8+
for i in range(len(s)):
9+
for j in range(i, len(s)):
10+
sub = s[i:j+1]
11+
if sub and sub == sub[::-1]:
12+
cnt.append(sub)
13+
return len(cnt)

reverse-bits/hu6r1s.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
"""
3+
binary로 변환 후 zfill로 32자리를 맞춰주고 reverse시킨다.
4+
"""
5+
def reverseBits(self, n: int) -> int:
6+
return int(bin(n)[2:].zfill(32)[::-1], 2)

0 commit comments

Comments
 (0)