diff --git a/find-minimum-in-rotated-sorted-array/YuuuuuuYu.java b/find-minimum-in-rotated-sorted-array/YuuuuuuYu.java new file mode 100644 index 0000000000..535f45c286 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/YuuuuuuYu.java @@ -0,0 +1,26 @@ +/** + * Runtime: 0ms + * Time Complexity: O(log n) + * + * Memory: 43.81MB + * Space Complexity: O(1) + * + * Approach: 이진 탐색 + */ +class Solution { + public int findMin(int[] nums) { + int left = 0; + int right = nums.length-1; + + while (left < right) { + int mid = left + (right-left)/2; + if (nums[mid] <= nums[right]) { + right = mid; + } else { + left = mid + 1; + } + } + + return nums[left]; + } +} diff --git a/maximum-depth-of-binary-tree/YuuuuuuYu.java b/maximum-depth-of-binary-tree/YuuuuuuYu.java new file mode 100644 index 0000000000..46eb3ff124 --- /dev/null +++ b/maximum-depth-of-binary-tree/YuuuuuuYu.java @@ -0,0 +1,20 @@ +/** + * Runtime: 0ms + * Time Complexity: O(n) + * + * Memory: 44.28MB + * Space Complexity: O(h) + * - h: 트리의 높이 or 최대 깊이 + * + * Approach: 재귀를 이용한 DFS 접근법 + * - 루트부터 시작하여 재귀가 시작할 때마다 1로 계산 + * - 트리의 끝에 도달하면 0을 리턴하여 차례대로 남은 깊이를 계산 + */ +class Solution { + public int maxDepth(TreeNode root) { + if (root == null) + return 0; + + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); + } +} diff --git a/merge-two-sorted-lists/YuuuuuuYu.java b/merge-two-sorted-lists/YuuuuuuYu.java new file mode 100644 index 0000000000..e0181563c6 --- /dev/null +++ b/merge-two-sorted-lists/YuuuuuuYu.java @@ -0,0 +1,34 @@ +/** + * Runtime: 0ms + * Time Complexity: O(n) + * - list1.length + list2.length + * + * Memory: 44.28MB + * Space Complexity: O(1) + * + * Approach: 빈 node를 만들고 list1, list2의 현재 값을 비교하여 더 작은 값을 추가 + * + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + if (list1 == null) return list2; + else if (list2 == null) return list1; + + ListNode result = new ListNode(); + ListNode currentNode = result; + while (list1 != null && list2 != null) { + if (list1.val > list2.val) { + currentNode.next = list2; + list2 = list2.next; + } else { + currentNode.next = list1; + list1 = list1.next; + } + + currentNode = currentNode.next; + } + + currentNode.next = list1 != null ? list1 : list2; + return result.next; + } +} diff --git a/word-search/YuuuuuuYu.java b/word-search/YuuuuuuYu.java new file mode 100644 index 0000000000..bcc64a9b17 --- /dev/null +++ b/word-search/YuuuuuuYu.java @@ -0,0 +1,67 @@ +/** + * Runtime: 128ms + * Time Complexity: O(m x n x 4^l) + * - m: board의 행 길이 + * - n: board의 열 길이 + * - l: word의 길이 + * + * Memory: 42.69MB + * Space Complexity: O(l) + * + * Approach: DFS + 백트래킹 + * 1) board를 순회하며 word의 첫 글자와 일치하는 글자를 찾음 + * 2) 일치하는 글자를 찾으면 DFS를 통해 상하좌우로 다음 글자를 찾음 + * 3) 성능 최적화 + * - 백트래킹을 위해 방문한 글자는 임시로 다른 문자('#')로 변경 (비트마스크) + * - charAt 캐싱 + */ +class Solution { + int[] dx = {-1, 1, 0, 0}; + int[] dy = {0, 0, -1, 1}; + + public boolean exist(char[][] board, String word) { + int m = board.length; + int n = board[0].length; + char startChar = word.charAt(0); + + for (int i=0; i= 0 && nx < board.length && + ny >= 0 && ny < board[0].length && + board[nx][ny] == nextChar) { + + if (dfs(board, nx, ny, index+1, word)) { + board[x][y] = temp; + return true; + } + } + } + + board[x][y] = temp; + return false; + } +}