diff --git a/coin-change/yuseok89.py b/coin-change/yuseok89.py new file mode 100644 index 0000000000..e4034370cd --- /dev/null +++ b/coin-change/yuseok89.py @@ -0,0 +1,24 @@ +# TC: O(amount * len(coins)) +# SC: O(amount) +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + + from collections import deque + + v = {0: 0} + q = deque([0]) + + while q and amount not in v: + + cur = q.popleft() + + for coin in coins: + + if cur + coin > amount or cur + coin in v: + continue + + v[cur + coin] = v[cur] + 1 + q.append(cur + coin) + + return -1 if amount not in v else v[amount] + diff --git a/find-minimum-in-rotated-sorted-array/yuseok89.py b/find-minimum-in-rotated-sorted-array/yuseok89.py new file mode 100644 index 0000000000..0ff09b0e55 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/yuseok89.py @@ -0,0 +1,20 @@ +# TC: O(logN) +# SC: O(1) +class Solution: + def findMin(self, nums: List[int]) -> int: + low = 0 + high = len(nums) - 1 + + while low < high - 1: + mid = (low + high) // 2 + + if nums[low] < nums[mid] < nums[high]: + return nums[low] + + if nums[low] > nums[mid]: + high = mid + else: + low = mid + + return min(nums[low], nums[high]) + diff --git a/maximum-depth-of-binary-tree/yuseok89.py b/maximum-depth-of-binary-tree/yuseok89.py new file mode 100644 index 0000000000..1ce2f54e8a --- /dev/null +++ b/maximum-depth-of-binary-tree/yuseok89.py @@ -0,0 +1,16 @@ +# TC: O(N) +# SC: O(H) +# 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: + + if not root: + return 0 + + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 + diff --git a/merge-two-sorted-lists/yuseok89.py b/merge-two-sorted-lists/yuseok89.py new file mode 100644 index 0000000000..a461ca9cbf --- /dev/null +++ b/merge-two-sorted-lists/yuseok89.py @@ -0,0 +1,26 @@ +# TC: O(N+M) +# SC: O(1) +# 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]: + + ret = ListNode() + cur = ret + + while list1 and list2: + if list1.val < list2.val: + cur.next = list1 + list1 = list1.next + else: + cur.next = list2 + list2 = list2.next + cur = cur.next + + cur.next = list1 if list1 else list2 + + return ret.next + diff --git a/word-search/yuseok89.py b/word-search/yuseok89.py new file mode 100644 index 0000000000..a1ec2bba3e --- /dev/null +++ b/word-search/yuseok89.py @@ -0,0 +1,37 @@ +# TC: O(N*M*4^L) +# SC: O(L) +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + + n = len(board) + m = len(board[0]) + + board_counter = Counter(board[r][c] for r in range(n) for c in range(m)) + word_counter = Counter(word) + + for c, cnt in word_counter.items(): + if board_counter[c] < cnt: + return False + + def rec(row, col, idx): + if idx == len(word): + return True + + if row < 0 or row >= n or col < 0 or col >= m or board[row][col] != word[idx]: + return False + + board[row][col] = None + + ret = rec(row + 1, col, idx + 1) or rec(row - 1, col, idx + 1) or rec(row, col + 1, idx + 1) or rec(row, col - 1, idx + 1) + + board[row][col] = word[idx] + + return ret + + for row in range(0, n): + for col in range(0, m): + if board[row][col] == word[0] and rec(row, col, 0): + return True + + return False +