From c82f16fa488fe2e178d13519570af593954cb767 Mon Sep 17 00:00:00 2001 From: sejineer Date: Sun, 20 Apr 2025 18:30:55 +0900 Subject: [PATCH 1/5] coin-change solution --- coin-change/sejineer.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 coin-change/sejineer.py 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 From 4f53d3ac6b34f87c2e694df48901a428dd1e51cf Mon Sep 17 00:00:00 2001 From: sejineer Date: Tue, 22 Apr 2025 23:35:42 +0900 Subject: [PATCH 2/5] word-search solution --- word-search/sejineer.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 word-search/sejineer.py 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 From caa6e7c4416c20d00d4d8f9acdc8deac198e5693 Mon Sep 17 00:00:00 2001 From: sejineer Date: Wed, 23 Apr 2025 21:40:56 +0900 Subject: [PATCH 3/5] find-minimum-in-rotated-sorted-array solution --- find-minimum-in-rotated-sorted-array/sejineer.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/sejineer.py 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] From 998436649384d051bbba08c1fae84af2a3dacce9 Mon Sep 17 00:00:00 2001 From: sejineer Date: Wed, 23 Apr 2025 22:14:13 +0900 Subject: [PATCH 4/5] maximum-depth-of-binary-tree solution --- maximum-depth-of-binary-tree/sejineer.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 maximum-depth-of-binary-tree/sejineer.py 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 From 0b89fcc2c9b5e5144611f6b4c3fdc55a03f7a4d0 Mon Sep 17 00:00:00 2001 From: sejineer Date: Sat, 26 Apr 2025 09:45:49 +0900 Subject: [PATCH 5/5] merge-two-sorted-lists solution --- merge-two-sorted-lists/sejineer.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 merge-two-sorted-lists/sejineer.py 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