diff --git a/coin-change/sejineer.py b/coin-change/sejineer.py new file mode 100644 index 000000000..383db1035 --- /dev/null +++ b/coin-change/sejineer.py @@ -0,0 +1,20 @@ +from collections import deque + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + queue = deque([(0, 0)]) + vis = set() + + while queue: + total, count = queue.popleft() + if total == amount: + return count + for coin in coins: + nxt = total + coin + if nxt > amount: + continue + if nxt in vis: + continue + vis.add(nxt) + queue.append((nxt, count + 1)) + return -1 diff --git a/find-minimum-in-rotated-sorted-array/sejineer.py b/find-minimum-in-rotated-sorted-array/sejineer.py new file mode 100644 index 000000000..22d80ff79 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/sejineer.py @@ -0,0 +1,15 @@ +""" +시간 복잡도: O(logN) +공간 복잡도: O(1) +""" +class Solution: + def findMin(self, nums: List[int]) -> int: + start, end = 0, len(nums) - 1 + + while start < end: + mid = (start + end) // 2 + if nums[mid] > nums[end]: + start = mid + 1 + else: + end = mid + return nums[start] diff --git a/maximum-depth-of-binary-tree/sejineer.py b/maximum-depth-of-binary-tree/sejineer.py new file mode 100644 index 000000000..7982a4341 --- /dev/null +++ b/maximum-depth-of-binary-tree/sejineer.py @@ -0,0 +1,19 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(h) h = 트리 높이 +""" +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + result = 0 + + def dfs(tree: Optional[TreeNode], depth: int): + nonlocal result + if tree == None: + result = max(result, depth) + return + + dfs(tree.left, depth + 1) + dfs(tree.right, depth + 1) + + dfs(root, 0) + return result diff --git a/merge-two-sorted-lists/sejineer.py b/merge-two-sorted-lists/sejineer.py new file mode 100644 index 000000000..ec93ef7ac --- /dev/null +++ b/merge-two-sorted-lists/sejineer.py @@ -0,0 +1,20 @@ +""" +시간 복잡도: O(n + m) +공간 복잡도: O(1) +""" +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + node = ListNode(0) + head = node + + while list1 and list2: + if list1.val <= list2.val: + head.next = list1 + list1 = list1.next + else: + head.next = list2 + list2 = list2.next + head = head.next + head.next = list1 if list1 else list2 + + return node.next diff --git a/word-search/sejineer.py b/word-search/sejineer.py new file mode 100644 index 000000000..3fbbb1c47 --- /dev/null +++ b/word-search/sejineer.py @@ -0,0 +1,33 @@ +""" +시간 복잡도: O(N * M * 4^(word_len)) +공간 복잡도: O(word_len) 재귀 스택 최대 깊이 word_len +""" +from collections import deque + +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + n, m = len(board), len(board[0]) + word_len = len(word) + + def dfs(x: int, y: int, idx: int) -> bool: + if idx == word_len: + return True + if x < 0 or y < 0 or x >= m or y >= n: + return False + if board[y][x] != word[idx]: + return False + + tmp, board[y][x] = board[y][x], '#' + for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): + if dfs(x + dx, y + dy, idx + 1): + return True + + board[y][x] = tmp + return False + + for i in range(n): + for j in range(m): + if board[i][j] == word[0] and dfs(j, i, 0): + return True + + return False