Skip to content

[yyyyyyyyyKim] WEEK 08 solutions #1496

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions clone-graph/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
# 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
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:

# 깊은 복사
# DFS 이용했으나 나중에 BFS로도 해 볼 것
# 시간복잡도 O(n), 공간복잡도 O(n)

if not node:
return None

# 원본에서 복사노드 매핑 저장할 딕셔너리
copied = {}

def dfs(curr):
# 현재 노드가 복사된 노드면 그대로 리턴
if curr in copied:
return copied[curr]

copy = Node(curr.val) # 현재 노드 복사
copied[curr] = copy # 복사본 저장

# 이웃노드들 복사해서 연결
for i in curr.neighbors:
copy.neighbors.append(dfs(i))

return copy

return dfs(node)
22 changes: 22 additions & 0 deletions longest-repeating-character-replacement/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def characterReplacement(self, s: str, k: int) -> int:

# 슬라이딩 윈도우(투포인터)
count = {} # 문자 빈도수
left = 0 # 왼쪽 포인터
max_count = 0 # 현재 윈도우에서 최대 문자 빈도수
answer = 0 # 가장 긴 동일 문자 부분 문자열의 최대 길이

# 오른쪽 포인터를 한 칸씩 늘려가며 윈도우 확장
for right in range(len(s)):
count[s[right]] = count.get(s[right],0) + 1 # 현재 문자 카운트 증가
max_count = max(max_count, count[s[right]]) # 가장 많이 등장한 문자 수 갱신

# 윈도우길이(right-left+1) - 가장자주나온문자 빈도수 > k : 현재 윈도우에서 바꿔야하는 문자가 더 많으면 왼쪽 포인터 이동
if (right-left+1) - max_count > k:
count[s[left]] -= 1 # 왼쪽 문자 제거
left += 1 # 왼쪽 포인터 오른쪽으로 한 칸 이동

answer = max(answer, right-left+1)

return answer
11 changes: 11 additions & 0 deletions reverse-bits/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def reverseBits(self, n: int) -> int:

answer = 0

for i in range(32):
answer <<= 1 # 왼쪽으로 한 칸 밀어서 자리 확보
answer |= (n&1) # n의 마지막 비트 추출해서 answer 맨 뒤에 추가
n >>= 1 # n 오른쪽으로 한 칸 밀기

return answer