From 818402e078c1180be008833b07ef92bf7bd97bb9 Mon Sep 17 00:00:00 2001 From: JEONGBEOM KO Date: Wed, 23 Apr 2025 00:30:19 +0900 Subject: [PATCH 1/5] add: merge two sorted lists solution --- merge-two-sorted-lists/JEONGBEOMKO.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 merge-two-sorted-lists/JEONGBEOMKO.java diff --git a/merge-two-sorted-lists/JEONGBEOMKO.java b/merge-two-sorted-lists/JEONGBEOMKO.java new file mode 100644 index 000000000..6e595ca2f --- /dev/null +++ b/merge-two-sorted-lists/JEONGBEOMKO.java @@ -0,0 +1,24 @@ +/** + * 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) { + if (list1 == null || list2 == null) { + return list1 == null ? list2 : list1; + } + if (list1.val < list2.val) { + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } else { + list2.next = mergeTwoLists(list2.next, list1); + return list2; + } + } +} From 2c75463af70886064a648dd1caf6043f18ede314 Mon Sep 17 00:00:00 2001 From: JEONGBEOM KO Date: Thu, 24 Apr 2025 23:54:03 +0900 Subject: [PATCH 2/5] add: maximum depth of binary tree solution --- maximum-depth-of-binary-tree/JEONGBEOMKO.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 maximum-depth-of-binary-tree/JEONGBEOMKO.java diff --git a/maximum-depth-of-binary-tree/JEONGBEOMKO.java b/maximum-depth-of-binary-tree/JEONGBEOMKO.java new file mode 100644 index 000000000..78fa69808 --- /dev/null +++ b/maximum-depth-of-binary-tree/JEONGBEOMKO.java @@ -0,0 +1,40 @@ +/** + * 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; + * } + * } + */ + + +class Solution { + /* + time complexity: O(n) + space complexity: O(h) + */ + int maxDepth = 0; + + public int maxDepth(TreeNode root) { + depthChk(root, 0); + return maxDepth; + } + + public void depthChk(TreeNode node, int depth) { + if (node == null) { + maxDepth = Math.max(depth, maxDepth); + return; + } + + // 트리의 좌우 탐색 + depthChk(node.left, depth + 1); + depthChk(node.right, depth + 1); + } +} From 62c54252fb8666426342e85ff5ba04a9be899899 Mon Sep 17 00:00:00 2001 From: JEONGBEOM KO Date: Fri, 25 Apr 2025 22:54:58 +0900 Subject: [PATCH 3/5] add: find minimum in rotated sorted array solution --- .../JEONGBEOMKO.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/JEONGBEOMKO.java diff --git a/find-minimum-in-rotated-sorted-array/JEONGBEOMKO.java b/find-minimum-in-rotated-sorted-array/JEONGBEOMKO.java new file mode 100644 index 000000000..fe144203d --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/JEONGBEOMKO.java @@ -0,0 +1,22 @@ +class Solution { + /* + time complexity: O(log n) + space complexity: O(h) + */ + public int findMin(int[] nums) { + int start = 0, end = nums.length - 1; + + if (nums[start] < nums[end]) return nums[start]; + + while (start < end) { + int mid = start + (end - start) / 2; + + if (nums[mid] < nums[end]) { + end = mid; + } else { + start = mid + 1; + } + } + return nums[start]; + } +} From 0c482e9632acee9ee00e3582087ab24b6d6734ed Mon Sep 17 00:00:00 2001 From: JEONGBEOM KO Date: Fri, 25 Apr 2025 23:22:21 +0900 Subject: [PATCH 4/5] add: Word Search solution --- word-search/JEONGBEOMKO.java | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 word-search/JEONGBEOMKO.java diff --git a/word-search/JEONGBEOMKO.java b/word-search/JEONGBEOMKO.java new file mode 100644 index 000000000..2b7a08c3e --- /dev/null +++ b/word-search/JEONGBEOMKO.java @@ -0,0 +1,43 @@ +public class Solution { + /* + time complexity: O(M × N × 3^L) + space complexity: O(M × N + L) + */ + private int rows, cols; + private boolean[][] visited; + private final int[] dx = {0, 1, 0, -1}; + private final int[] dy = {1, 0, -1, 0}; + + public boolean exist(char[][] board, String word) { + rows = board.length; + cols = board[0].length; + visited = new boolean[rows][cols]; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (dfs(board, word, i, j, 0)) { + return true; + } + } + } + return false; + } + + private boolean dfs(char[][] board, String word, int x, int y, int idx) { + if (idx == word.length()) return true; + + if (x < 0 || y < 0 || x >= rows || y >= cols) return false; + if (visited[x][y] || board[x][y] != word.charAt(idx)) return false; + + visited[x][y] = true; + + for (int d = 0; d < 4; d++) { + if (dfs(board, word, x + dx[d], y + dy[d], idx + 1)) { + return true; + } + } + + visited[x][y] = false; // 백트래킹 + return false; + } +} From 002434663ede2b72bcc4c07fbc33cb535455bc05 Mon Sep 17 00:00:00 2001 From: JEONGBEOM KO Date: Fri, 25 Apr 2025 23:41:39 +0900 Subject: [PATCH 5/5] add: Coin Change --- coin-change/JEONGBEOMKO.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 coin-change/JEONGBEOMKO.java diff --git a/coin-change/JEONGBEOMKO.java b/coin-change/JEONGBEOMKO.java new file mode 100644 index 000000000..de7fb4c6e --- /dev/null +++ b/coin-change/JEONGBEOMKO.java @@ -0,0 +1,19 @@ +class Solution { + /* + time complexity: O(amount × n) + space complexity: O(amount) + */ + public int coinChange(int[] coins, int amount) { + int[] memo = new int[amount + 1]; + Arrays.fill(memo, amount + 1); // 초기값: 만들 수 없는 큰 수 + memo[0] = 0; // 0원을 만들기 위한 동전 수는 0개 + + for (int coin : coins) { + for (int i = coin; i <= amount; i++) { + memo[i] = Math.min(memo[i], memo[i - coin] + 1); + } + } + + return memo[amount] > amount ? -1 : memo[amount]; + } +}