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
14 changes: 14 additions & 0 deletions find-minimum-in-rotated-sorted-array/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
# 시간복잡도 O(logn) 제약사항이 있으므로 binary search 사용
left = 0
right = len(nums) - 1
# rotated sort 이므로 mid와 right 비교
# mid 와 left 비교시 최소값이 어딨는지 특정할 수가 없음 (예외케이스 발생)
while left < right:
mid = (left + right) // 2
if nums[mid] < nums[right]:
right = mid
else:
left = mid + 1
return nums[left]
23 changes: 23 additions & 0 deletions maximum-depth-of-binary-tree/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
# root가 없는경우 depth 0
if root is None:
return 0
# depth를 같이 포함시킴
# dfs의 경우 각 경로별로 끝까지 탐색하는데, 각 경로마다 depth가 다르므로 표기하면서 stack에 추가해야함
stack = [[root, 1]]
max_depth = 1
while stack:
node, cur_depth = stack.pop()
max_depth = max(max_depth, cur_depth)
if node.left:
stack.append([node.left, cur_depth + 1])
if node.right:
stack.append([node.right, cur_depth + 1])
return max_depth
30 changes: 30 additions & 0 deletions merge-two-sorted-lists/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# head of merged linked list를 반환해야 하므로 head를 임시로 생성
head = ListNode()
pointer = head

# 정렬된 링크드 리스트이므로 하나라도 끝까지 pointer를 이동해서 None이 될때까지 loop
# 시간복잡도 O(n), 공간복잡도 O(1)
while list1 and list2:
# val 비교해서 next 지정
if list1.val <= list2.val:
pointer.next = list1
list1 = list1.next
else:
pointer.next = list2
list2 = list2.next
# pointer도 next로 이동
pointer = pointer.next
# 남은 리스트의 경우 정렬되어 있으므로 그대로 연결
if list1:
pointer.next = list1
else:
pointer.next = list2

return head.next
25 changes: 25 additions & 0 deletions word-search/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
visited = set()

def dfs(row, col, idx):
# 끝까지 탐색시 길이와 k가 같아지므로 True 반환
if idx == len(word):
return True
# 범위를 벗어난 경우, 방문한 경우, 같은 word가 없는 경우 False 반환
if not (0 <= row < rows) or not (0 <= col < cols) or (row, col) in visited or board[row][col] != word[idx]:
return False

visited.add((row, col))
res = dfs(row + 1, col, idx + 1) or dfs(row - 1, col, idx + 1) or dfs(row, col + 1, idx + 1) or dfs(row, col - 1, idx + 1)
visited.remove((row, col))
return res
# 시작점 탐색
for row in range(rows):
for col in range(cols):
if dfs(row, col, 0):
return True
# 시간복잡도 O(rows*cols*4^(word))
# 공간복잡도 O(rows*cols)
return False