From 9108044923ae3228e94b863fce55c9809919dee3 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 16 Aug 2025 12:36:43 +0900 Subject: [PATCH] Week4 Solutions --- coin-change/hoyeongkwak.java | 21 +++++++++ .../hoyeongkwak.java | 20 ++++++++ maximum-depth-of-binary-tree/hoyeongkwak.java | 27 +++++++++++ merge-two-sorted-lists/hoyeongkwak.java | 47 +++++++++++++++++++ word-search/hoyeongkwak.java | 47 +++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 coin-change/hoyeongkwak.java create mode 100644 find-minimum-in-rotated-sorted-array/hoyeongkwak.java create mode 100644 maximum-depth-of-binary-tree/hoyeongkwak.java create mode 100644 merge-two-sorted-lists/hoyeongkwak.java create mode 100644 word-search/hoyeongkwak.java diff --git a/coin-change/hoyeongkwak.java b/coin-change/hoyeongkwak.java new file mode 100644 index 0000000000..26315adeba --- /dev/null +++ b/coin-change/hoyeongkwak.java @@ -0,0 +1,21 @@ +/* +Time Complexity : O(n) +Space Complexity : O(n) +*/ + +class Solution { + public int coinChange(int[] coins, int amount) { + if (amount == 0) return 0; + int[] dp = new int[amount + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + for (int coin : coins) { + for (int i = coin; i <= amount; i++) { + if (dp[i - coin] != Integer.MAX_VALUE) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + } + return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount]; + } +} diff --git a/find-minimum-in-rotated-sorted-array/hoyeongkwak.java b/find-minimum-in-rotated-sorted-array/hoyeongkwak.java new file mode 100644 index 0000000000..97ae873f4e --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/hoyeongkwak.java @@ -0,0 +1,20 @@ +/* +Time Complexity : O(log n) +Space Complexity : O(1) +*/ +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]) { + left = mid + 1; + } else { + right = mid; + } + } + return nums[left]; + } +} diff --git a/maximum-depth-of-binary-tree/hoyeongkwak.java b/maximum-depth-of-binary-tree/hoyeongkwak.java new file mode 100644 index 0000000000..70c9b1a3cb --- /dev/null +++ b/maximum-depth-of-binary-tree/hoyeongkwak.java @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +/* +Time Complexity : O(n) +Space Complexity : O(log n) +*/ +class Solution { + public int maxDepth(TreeNode root) { + if (root == null) return 0; + int maxLeft = maxDepth(root.left); + int maxRight = maxDepth(root.right); + return Math.max(maxLeft, maxRight) + 1; + } +} diff --git a/merge-two-sorted-lists/hoyeongkwak.java b/merge-two-sorted-lists/hoyeongkwak.java new file mode 100644 index 0000000000..8142915a47 --- /dev/null +++ b/merge-two-sorted-lists/hoyeongkwak.java @@ -0,0 +1,47 @@ +/* +Time Complexity : O(m + n) +Space Complexity : O(1) +*/ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode tempNode = new ListNode(0); + ListNode current = tempNode; + + while (list1 != null && list2 != null) { + if (list1.val <= list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + + current.next = (list1 != null) ? list1 : list2; + return tempNode.next; + // if (list1 == null) { + // return list2; + // } + // if (list2 == null) { + // return list1; + // } + // if (list1.val < list2.val) { + // list1.next = mergeTwoLists(list1.next, list2); + // return list1; + // } else { + // list2.next = mergeTwoLists(list1, list2.next); + // return list2; + // } + } +} diff --git a/word-search/hoyeongkwak.java b/word-search/hoyeongkwak.java new file mode 100644 index 0000000000..fd453262e9 --- /dev/null +++ b/word-search/hoyeongkwak.java @@ -0,0 +1,47 @@ +/* +n : 셀 수 +L : 단어 길이 +Time Complexity : O(n * 4^L) +Space Complexity : O(L) +*/ + +class Solution { + public boolean exist(char[][] board, String word) { + if (board == null || board.length == 0 || board[0].length == 0) return false; + int rows = board.length; + int cols = board[0].length; + boolean[][] visited = new boolean[rows][cols]; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (backtrack(board, word, i, j, 0, visited)) { + return true; + } + } + } + return false; + } + + private boolean backtrack(char[][] board, String word, int row, int col, int index, boolean[][] visited) { + if (index == word.length()) return true; + if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || visited[row][col] || board[row][col] != word.charAt(index)) { + return false; + } + visited[row][col] = true; + + int[] dx = {-1, 1, 0, 0}; + int[] dy = {0, 0, -1, 1}; + + for (int i = 0; i < 4; i++) { + int newRow = row + dx[i]; + int newCol = col + dy[i]; + + if (backtrack(board, word, newRow, newCol, index + 1, visited)) { + visited[row][col] = false; + return true; + } + } + visited[row][col] = false; + return false; + } +}