Skip to content

Commit 120109b

Browse files
authored
Merge pull request #1506 from shinsj4653/main
[shinsj4653] Week 08 Solutions
2 parents ce12735 + 2544818 commit 120109b

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

clone-graph/shinsj4653.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
[문제풀이]
3+
# Inputs
4+
5+
# Outputs
6+
7+
# Constraints
8+
9+
# Ideas
10+
11+
[회고]
12+
13+
"""
14+
15+
16+
# Definition for a Node.
17+
class Node:
18+
def __init__(self, val=0, neighbors=None):
19+
self.val = val
20+
self.neighbors = neighbors if neighbors is not None else []
21+
22+
23+
from typing import Optional
24+
from collections import deque
25+
26+
27+
class Solution:
28+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
29+
30+
if not node:
31+
return
32+
33+
clone = Node(node.val)
34+
clones = {node: clone}
35+
36+
q = deque([node]) # 해당 라인 답지 참고
37+
38+
while q:
39+
node = q.popleft()
40+
41+
for nei in node.neighbors:
42+
if nei not in clones:
43+
clones[nei] = Node(nei.val) # 답지 참고
44+
q.append(nei)
45+
46+
clones[node].neighbors.append(clones[nei]) # 답지 참고
47+
48+
return clone
49+
50+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
[문제풀이]
3+
# Inputs
4+
5+
# Outputs
6+
7+
# Constraints
8+
9+
# Ideas
10+
11+
[회고]
12+
13+
"""
14+
15+
16+
class Solution:
17+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
18+
n, m = len(text1), len(text2)
19+
dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)]
20+
21+
for i in range(1, n + 1):
22+
for j in range(1, m + 1):
23+
if text1[i - 1] == text2[j - 1]:
24+
dp[i][j] = dp[i - 1][j - 1] + 1
25+
26+
else:
27+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
28+
29+
return dp[n][m]
30+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
[문제풀이]
3+
# Inputs
4+
5+
# Outputs
6+
7+
# Constraints
8+
9+
# Ideas
10+
11+
[회고]
12+
13+
"""
14+
class Solution:
15+
def characterReplacement(self, s: str, k: int) -> int:
16+
max_len = 0
17+
counter = {}
18+
start, end = 0, 0
19+
while end < len(s):
20+
counter[s[end]] = counter.get(s[end], 0) + 1
21+
while end - start + 1 - max(counter.values()) > k:
22+
counter[s[start]] -= 1
23+
start += 1
24+
max_len = max(end - start + 1, max_len)
25+
end += 1
26+
return max_len
27+

palindromic-substrings/shinsj4653.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
[문제풀이]
3+
# Inputs
4+
string s
5+
6+
# Outputs
7+
the number of palindromic substrings
8+
9+
# Constraints
10+
1 <= s.length <= 1000
11+
s consists of lowercase English letters.
12+
13+
# Ideas
14+
부분 문자열 중 팰린드롬 인거
15+
순열?
16+
10^3 => 시초 예상
17+
18+
코드를 짜보니 O(n^3) 나오긴하는데 우선 정답
19+
20+
[회고]
21+
22+
"""
23+
24+
25+
class Solution:
26+
def countSubstrings(self, s: str) -> int:
27+
ret = 0
28+
29+
for num in range(1, len(s) + 1):
30+
for i in range(len(s) - num + 1):
31+
ss = s[i:i + num]
32+
if ss == ss[::-1]:
33+
ret += 1
34+
35+
return ret
36+
37+
# 해설보고 스스로 풀이
38+
39+
class Solution:
40+
def countSubstrings(self, s: str) -> int:
41+
dp = {}
42+
43+
for start in range(len(s)):
44+
for end in range(start, -1, -1):
45+
if start == end:
46+
dp[(start, end)] = True
47+
48+
elif start + 1 == end:
49+
dp[(start, end)] = s[start] == s[end]
50+
51+
else:
52+
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end]
53+
54+
return dp.values().count(True)
55+
56+
# 기존 값 재활용하려면 end 부터 세야하는게 이해가 안감
57+
# -> 다시 풀이
58+
class Solution:
59+
def countSubstrings(self, s: str) -> int:
60+
dp = {}
61+
62+
for end in range(len(s)):
63+
for start in range(end, -1, -1):
64+
if start == end:
65+
dp[(start, end)] = True
66+
67+
elif start + 1 == end:
68+
dp[(start, end)] = s[start] == s[end]
69+
70+
else:
71+
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end]
72+
73+
return list(dp.values()).count(True)
74+

reverse-bits/shinsj4653.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
[문제풀이]
3+
# Inputs
4+
5+
# Outputs
6+
7+
# Constraints
8+
9+
# Ideas
10+
11+
[회고]
12+
13+
"""
14+
15+
16+
class Solution:
17+
def reverseBits(self, n: int) -> int:
18+
st = []
19+
20+
# while n > 0:
21+
# st.append(n % 2)
22+
# n //= 2 => 32 bit 길이 맞춰야함!
23+
24+
while len(st) < 32:
25+
print('st.append: ', n % 2)
26+
st.append(n % 2)
27+
n //= 2
28+
29+
ret, num = 0, 0
30+
print("st: ", st)
31+
32+
# 6 : 110
33+
# [0 1 1]
34+
35+
while st:
36+
print('st.pop(): ', st[-1])
37+
ret += st.pop() * (2 ** num)
38+
num += 1
39+
40+
return ret
41+
42+

0 commit comments

Comments
 (0)