diff --git a/coin-change/daiyongg-kim.py b/coin-change/daiyongg-kim.py new file mode 100644 index 0000000000..f1b51a9004 --- /dev/null +++ b/coin-change/daiyongg-kim.py @@ -0,0 +1,26 @@ +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + if amount == 0: + return 0 + + level = [0] + visited = {0} + count = 0 + + while level: + count += 1 + next_level = [] + + for current_amount in level: + for coin in coins: + new_amount = current_amount + coin + + if new_amount == amount: + return count + + if new_amount < amount and new_amount not in visited: + visited.add(new_amount) + next_level.append(new_amount) + level = next_level + + return -1 diff --git a/find-minimum-in-rotated-sorted-array/daiyongg-kim.py b/find-minimum-in-rotated-sorted-array/daiyongg-kim.py new file mode 100644 index 0000000000..110de379e0 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/daiyongg-kim.py @@ -0,0 +1,14 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + #constraint complexity: O(log n) + left = 0 + right = len(nums) - 1 + + while left < right: + mid = (left + right) // 2 + + if nums[mid] < nums[right]: + right = mid + else: + left = mid + 1 + return nums[left] diff --git a/maximum-depth-of-binary-tree/daiyongg-kim.py b/maximum-depth-of-binary-tree/daiyongg-kim.py new file mode 100644 index 0000000000..c31ce33f1c --- /dev/null +++ b/maximum-depth-of-binary-tree/daiyongg-kim.py @@ -0,0 +1,27 @@ +class Solution: + + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + queue = deque([root]) + depth = 0 + + while queue: + depth += 1 + print(len(queue)) + for _ in range(len(queue)): + node = queue.popleft() + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + return depth + + +# if not root: +# return 0 + +# left_depth = self.maxDepth(root.left) +# right_depth = self.maxDepth(root.right) + +# return max(left_depth, right_depth) + 1 diff --git a/merge-two-sorted-lists/daiyongg-kim.py b/merge-two-sorted-lists/daiyongg-kim.py new file mode 100644 index 0000000000..aacc307de7 --- /dev/null +++ b/merge-two-sorted-lists/daiyongg-kim.py @@ -0,0 +1,38 @@ +# 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 list1 is None: + return list2 + if list2 is None: + return list1 + + if list1.val < list2.val: + list1.next = self.mergeTwoLists(list1.next, list2) + return list1 + else: + list2.next = self.mergeTwoLists(list1, list2.next) + return list2 + +# class Solution: +# def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: +# result = ListNode(-1) +# dummy = result +# while list1 and list2: +# if list1.val < list2.val: +# dummy.next = list1 +# list1 = list1.next +# else: +# dummy.next = list2 +# list2 = list2.next +# dummy = dummy.next + +# if list1: +# dummy.next = list1 +# else: +# dummy.next = list2 + +# return result.next diff --git a/word-search/daiyongg-kim.py b/word-search/daiyongg-kim.py new file mode 100644 index 0000000000..37428225ea --- /dev/null +++ b/word-search/daiyongg-kim.py @@ -0,0 +1,28 @@ +class Solution: + + def exist(self, board: List[List[str]], word: str) -> bool: + row = len(board) + col = len(board[0]) + + def dfs(i: int, j: int, k: int): + if k == len(word): + return True + + if i < 0 or i >= row or j < 0 or j >= col or board[i][j] != word[k]: + return False + + current = board[i][j] + board[i][j] = '' + + if dfs(i-1, j, k+1) or dfs(i+1, j, k+1) or dfs(i, j-1, k+1) or dfs(i, j+1, k+1): + return True + + board[i][j] = current + return False + + for i in range(row): + for j in range(col): + if dfs(i, j, 0): + return True + + return False