diff --git a/find-minimum-in-rotated-sorted-array/Chanz82.py b/find-minimum-in-rotated-sorted-array/Chanz82.py new file mode 100644 index 0000000000..514b4db712 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/Chanz82.py @@ -0,0 +1,15 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + + left = 0 + right = len(nums) - 1 + + while left < right: + mid = (left + right) // 2 + + if nums[right] < nums[mid]: # right 와 mid 사이에 최소 값이 존재 + left = mid + 1 + else: + right = mid + + return nums[left] diff --git a/maximum-depth-of-binary-tree/Chanz82.py b/maximum-depth-of-binary-tree/Chanz82.py new file mode 100644 index 0000000000..e15a4b4d4a --- /dev/null +++ b/maximum-depth-of-binary-tree/Chanz82.py @@ -0,0 +1,25 @@ +# 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: + + max_depth = 0 + def dfs(cur_node, cur_depth): + nonlocal max_depth + if cur_node == None: + return + cur_depth += 1 + max_depth = max(max_depth, cur_depth) + + dfs(cur_node.left, cur_depth) + dfs(cur_node.right, cur_depth) + return + + dfs(root, 0) + return max_depth + diff --git a/merge-two-sorted-lists/Chanz82.py b/merge-two-sorted-lists/Chanz82.py new file mode 100644 index 0000000000..9dc7ef8536 --- /dev/null +++ b/merge-two-sorted-lists/Chanz82.py @@ -0,0 +1,33 @@ +# 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]: + + if not list1: + return list2 + if not list2: + return list1 + + cursor = ListNode() + result = cursor + cursor.next = list1 + + while cursor.next: + while list2 and list2.val <= cursor.next.val: + temp_1 = cursor.next + temp_2 = list2.next + + cursor.next = list2 + list2.next = temp_1 + list2 = temp_2 + + cursor = cursor.next + + if list2: + cursor.next = list2 + + return result.next + diff --git a/word-search/Chanz82.py b/word-search/Chanz82.py new file mode 100644 index 0000000000..c577fad99d --- /dev/null +++ b/word-search/Chanz82.py @@ -0,0 +1,32 @@ +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + + def dfs(current, word_idx): + x, y = current + + if word_idx == len(word): + return True + + if x < 0 or x >= len(board) or y < 0 or y >= len(board[0]): + return False + + if board[x][y] != word[word_idx] : + return False + + tmp = board[x][y] + board[x][y] = '#' + + found = (dfs((x-1, y), word_idx+1) or + dfs((x+1, y), word_idx+1) or + dfs((x, y-1), word_idx+1) or + dfs((x, y+1), word_idx+1)) + + board[x][y] = tmp + + return found + + for x, rows in enumerate(board): + for y, cell in enumerate(rows): + if dfs((x, y), 0) == True : + return True + return False