Skip to content
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
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)
18 changes: 18 additions & 0 deletions longest-common-subsequence/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# DP (시간복잡도 : O(len(text1)*len(text2)), 공간복잡도 : O(len(text1)*len(text2)))
# dp[i][j] : text1의 앞i글자와 text2의 앞j글자 사이의 LCS(가장긴공통부분수열)길이
dp = [[0]*(len(text2)+1) for _ in range(len(text1)+1)]

for i in range(1, len(text1)+1):
for j in range(1, len(text2)+1):

# 현재 문자가 같다면, 이전 대각선 값에 +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[len(text1)][len(text2)]
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
17 changes: 17 additions & 0 deletions palindromic-substrings/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def countSubstrings(self, s: str) -> int:

# DP
dp = [[False]*(len(s)) for _ in range(len(s))] # dp 테이블 초기화(dp[i][j]=s[i:j+1]의 팰린드롬 여부)
answer = 0

# j = 끝글자인덱스, i = 처음글자인덱스
for j in range(len(s)):
for i in range(j+1):
# 1. s[i](첫글자)와 s[j](끝글자)가 같은 글자이고,
# 2. j-i <= 2, 즉 글자 길이가 1~3일때는 무조건 팰리드롬 /아니면 s[i+1][j-1](가운데글자들)이 팰린드룸이면 s[i]부터 s[j]까지 팰린드룸.
if s[i] == s[j] and (j-i <= 2 or dp[i+1][j-1]):
dp[i][j] = True # s[i]에서 s[j]는 팰린드룸(True)
answer += 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