Skip to content

[shinsj4653] Week 08 Solutions #1506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions clone-graph/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


# Definition for a Node.
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []


from typing import Optional
from collections import deque


class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:

if not node:
return

clone = Node(node.val)
clones = {node: clone}

q = deque([node]) # 해당 라인 답지 참고

while q:
node = q.popleft()

for nei in node.neighbors:
if nei not in clones:
clones[nei] = Node(nei.val) # 답지 참고
q.append(nei)

clones[node].neighbors.append(clones[nei]) # 답지 참고

return clone


30 changes: 30 additions & 0 deletions longest-common-subsequence/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n, m = len(text1), len(text2)
dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)]

for i in range(1, n + 1):
for j in range(1, m + 1):
if text1[i - 1] == text2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1

else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

return dp[n][m]

27 changes: 27 additions & 0 deletions longest-repeating-character-replacement/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
max_len = 0
counter = {}
start, end = 0, 0
while end < len(s):
counter[s[end]] = counter.get(s[end], 0) + 1
while end - start + 1 - max(counter.values()) > k:
counter[s[start]] -= 1
start += 1
max_len = max(end - start + 1, max_len)
end += 1
return max_len

74 changes: 74 additions & 0 deletions palindromic-substrings/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
[문제풀이]
# Inputs
string s

# Outputs
the number of palindromic substrings

# Constraints
1 <= s.length <= 1000
s consists of lowercase English letters.

# Ideas
부분 문자열 중 팰린드롬 인거
순열?
10^3 => 시초 예상

코드를 짜보니 O(n^3) 나오긴하는데 우선 정답

[회고]

"""


class Solution:
def countSubstrings(self, s: str) -> int:
ret = 0

for num in range(1, len(s) + 1):
for i in range(len(s) - num + 1):
ss = s[i:i + num]
if ss == ss[::-1]:
ret += 1

return ret

# 해설보고 스스로 풀이

class Solution:
def countSubstrings(self, s: str) -> int:
dp = {}

for start in range(len(s)):
for end in range(start, -1, -1):
if start == end:
dp[(start, end)] = True

elif start + 1 == end:
dp[(start, end)] = s[start] == s[end]

else:
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end]

return dp.values().count(True)

# 기존 값 재활용하려면 end 부터 세야하는게 이해가 안감
# -> 다시 풀이
class Solution:
def countSubstrings(self, s: str) -> int:
dp = {}

for end in range(len(s)):
for start in range(end, -1, -1):
if start == end:
dp[(start, end)] = True

elif start + 1 == end:
dp[(start, end)] = s[start] == s[end]

else:
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end]

return list(dp.values()).count(True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp의 값을 가져와 리스트로 변환하는 방법외에 단순하게 count 변수 하나를 for 루프 위에 두고 dp[(start, end)] = True가 될 때마다 1씩 증가시키면 메모리나 시간을 절약해 볼 수 있을 것 같기도 합니다!


42 changes: 42 additions & 0 deletions reverse-bits/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


class Solution:
def reverseBits(self, n: int) -> int:
st = []

# while n > 0:
# st.append(n % 2)
# n //= 2 => 32 bit 길이 맞춰야함!

while len(st) < 32:
print('st.append: ', n % 2)
st.append(n % 2)
n //= 2

ret, num = 0, 0
print("st: ", st)

# 6 : 110
# [0 1 1]

while st:
print('st.pop(): ', st[-1])
ret += st.pop() * (2 ** num)
num += 1

return ret