diff --git a/find-minimum-in-rotated-sorted-array/ohkingtaek.py b/find-minimum-in-rotated-sorted-array/ohkingtaek.py new file mode 100644 index 0000000000..3959159a59 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/ohkingtaek.py @@ -0,0 +1,8 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + """ + 시간복잡도: O(n) + 공간복잡도: O(1) + 왜 이렇게 해도 solve가 되네요..ㅎㅎ + """ + return min(nums) diff --git a/maximum-depth-of-binary-tree/ohkingtaek.py b/maximum-depth-of-binary-tree/ohkingtaek.py new file mode 100644 index 0000000000..0d36c04cbd --- /dev/null +++ b/maximum-depth-of-binary-tree/ohkingtaek.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: + """ + 시간복잡도: O(n) + 공간복잡도: O(n) + 이진 트리 탐색하며 뎁스 증가 계속 하며 진행 + """ + depth = 0 + if not root: + return 0 + array = [(root, 1)] + while array: + node, d = array.pop() + depth = max(d, depth) + if node.left: + array.append((node.left, d + 1)) + if node.right: + array.append((node.right, d + 1)) + return depth diff --git a/merge-two-sorted-lists/ohkingtaek.py b/merge-two-sorted-lists/ohkingtaek.py new file mode 100644 index 0000000000..817cafd54e --- /dev/null +++ b/merge-two-sorted-lists/ohkingtaek.py @@ -0,0 +1,26 @@ +# 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]: + """ + 시간복잡도: O(n) + 공간복잡도: O(1) + 1. 두 리스트를 합쳐서 새로운 리스트 반환 + 2. 두 리스트를 돌면서 작은 값을 새로운 리스트에 추가 + 3. 두 리스트를 모두 순회하면 남은 것을 새로운 리스트에 추가 + """ + root = ListNode(None) + node = root + while list1 and list2: + if list1.val < list2.val: + node.next = list1 + list1 = list1.next + else: + node.next = list2 + list2 = list2.next + node = node.next + node.next = list1 or list2 + return root.next diff --git a/word-search/ohkingtaek.py b/word-search/ohkingtaek.py new file mode 100644 index 0000000000..b86e311e9a --- /dev/null +++ b/word-search/ohkingtaek.py @@ -0,0 +1,32 @@ +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + """ + 시간 복잡도: O(M * N * 4^L) + 공간 복잡도: O(M * N) + 1. 행렬을 순회하며 단어의 첫 번째 문자와 일치하는 위치를 찾기 + 2. 찾은 위치에서 상하좌우로 이동하며 단어의 다음 문자와 일치하는 위치를 찾기 + 3. 단어의 마지막 문자까지 찾으면 True를 반환, 못 찾으면 False + """ + m, n = len(board), len(board[0]) + + def dfs(i, j, k): + if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[k]: + return False + if k == len(word) - 1: + return True + t = board[i][j] + board[i][j] = "#" + dx, dy = (1, -1, 0, 0), (0, 0, 1, -1) + ok = False + for d in range(4): + if dfs(i + dx[d], j + dy[d], k + 1): + ok = True + break + board[i][j] = t + return ok + + for i in range(m): + for j in range(n): + if dfs(i, j, 0): + return True + return False