From 95ca7a4831dbaa5459f89a18a40f71bb9ea06b8b Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Thu, 24 Apr 2025 22:33:02 +0900 Subject: [PATCH 1/5] merge two sorted lists solution --- merge-two-sorted-lists/Yn3-3xh.java | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 merge-two-sorted-lists/Yn3-3xh.java diff --git a/merge-two-sorted-lists/Yn3-3xh.java b/merge-two-sorted-lists/Yn3-3xh.java new file mode 100644 index 000000000..fc1d00abd --- /dev/null +++ b/merge-two-sorted-lists/Yn3-3xh.java @@ -0,0 +1,37 @@ +/** +[문제풀이] +time: O(N + M), space: O(N + M) +- list1과 list2를 비교해서 list2의 수가 작으면 list1을 list2로 교체하자. +- 이때 list1은 list2의 자리에 들어갈 것이다. + +[회고] +주어진 메서드를 dfs로 활용할 수도 있구나! + */ + +/** + * 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) { + ListNode temp = list1; + list1 = list2; + list2 = temp; + } + + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } +} + From 2af5762c3f4f977b8bd649f6ec1059f77c7a0409 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Thu, 24 Apr 2025 23:01:24 +0900 Subject: [PATCH 2/5] maximum-depth-of-binary-tree solution --- maximum-depth-of-binary-tree/Yn3-3xh.java | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 maximum-depth-of-binary-tree/Yn3-3xh.java diff --git a/maximum-depth-of-binary-tree/Yn3-3xh.java b/maximum-depth-of-binary-tree/Yn3-3xh.java new file mode 100644 index 000000000..621831ec8 --- /dev/null +++ b/maximum-depth-of-binary-tree/Yn3-3xh.java @@ -0,0 +1,55 @@ +/** +[문제풀이] +- 최대 depth 구하기 +- left or right의 null일 때 depth의 max 구하기 +- DFS 1 +time: O(N), space: O(N) + class Solution { + public int maxDepth(TreeNode root) { + return nextNode(root, 0); + } + + private int nextNode(TreeNode node, int depth) { + if (node == null) { + return depth; + } + + int leftDepth = nextNode(node.left, depth + 1); + int rightDepth = nextNode(node.right, depth + 1); + return Math.max(leftDepth, rightDepth); + } + } +- DFS 2 +time: O(N), space: O(N) + +[회고] +이전에 풀었던 Merget Two Sorted Lists 문제에서 주어진 메서드를 재사용할 수 있겠다는 생각으로 풀 수 있었다! + */ + +/** + * 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 { + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + + int leftDepth = maxDepth(root.left); + int rightDepth = maxDepth(root.right); + return Math.max(leftDepth, rightDepth) + 1; + } +} + From 5ba1877f854984d2205b4dcc803d6ac37f2ea43c Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Thu, 24 Apr 2025 23:58:46 +0900 Subject: [PATCH 3/5] find minimum in rotated sorted array solution --- .../Yn3-3xh.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/Yn3-3xh.java diff --git a/find-minimum-in-rotated-sorted-array/Yn3-3xh.java b/find-minimum-in-rotated-sorted-array/Yn3-3xh.java new file mode 100644 index 000000000..7a0c5434f --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/Yn3-3xh.java @@ -0,0 +1,49 @@ +/** +[문제풀이] +- 왼쪽의 수 보다 오른쪽의 수가 작은 순간을 찾자. +- 가장 왼쪽의 수 보다 가장 오른쪽의 수가 크면 회전이 일어나지 않은 상태이므로, 첫번째가 최솟값이다. +- 풀이1 +time: O(N), space: O(1); + class Solution { + public int findMin(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + + for (int i = 1; i < nums.length; i++) { + if (nums[i - 1] > nums[i]) { + return nums[i]; + } + } + return nums[0]; + } + } +- 풀이2 <이진탐색> +time: O(log N), space: O(1) + +[회고] +이진탐색으로 푸는 능력이 부족한 것 같다. + */ + +class Solution { + public int findMin(int[] nums) { + int left = 0; + int right = nums.length - 1; + + if (nums[left] < nums[right]) { + return nums[left]; + } + + while (left < right) { + int mid = (left + right) / 2; + + if (nums[mid] <= nums[right]) { + right = mid; + } else { + left = mid + 1; + } + } + return nums[left]; + } +} + From 87a2701c1257398741c98225bceddf6f7f75ff49 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Fri, 25 Apr 2025 22:26:21 +0900 Subject: [PATCH 4/5] word search solution --- word-search/Yn3-3xh.java | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 word-search/Yn3-3xh.java diff --git a/word-search/Yn3-3xh.java b/word-search/Yn3-3xh.java new file mode 100644 index 000000000..2d76cdfc0 --- /dev/null +++ b/word-search/Yn3-3xh.java @@ -0,0 +1,63 @@ +/** +[문제풀이] +- 이웃되는 알파벳이 이어져야 한다. +- 각 알파벳은 한번만 호출되어야 한다. +- DFS +time: O(N M 4^L), space: O(L) + +[회고] +BFS로 풀면 되지 않을까 했지만, 최단경로를 찾는 것이 아니니 안 어울릴 것 같다. +DFS로 풀자는 방법은 맞았지만, 풀이과정이 어려웠다. +다시 풀어보면 좋을 것 같다. + */ + +class Solution { + public boolean exist(char[][] board, String word) { + int column = board.length; + int row = board[0].length; + boolean[][] visited = new boolean[column][row]; + + for (int i = 0; i < column; i++) { + for (int j = 0; j < row; j++) { + if (board[i][j] == word.charAt(0)) { + if (dfs(board, word, visited, i, j, 0)) { + return true; + } + } + } + } + return false; + } + + private boolean dfs(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) { + if (wordIndex == word.length()) { + return true; + } + + if (isVisited(board, word, visited, i, j, wordIndex)) { + return false; + } + + visited[i][j] = true; + if (dfs(board, word, visited, i + 1, j, wordIndex + 1) || + dfs(board, word, visited, i - 1, j, wordIndex + 1) || + dfs(board, word, visited, i, j + 1, wordIndex + 1) || + dfs(board, word, visited, i, j - 1, wordIndex + 1) + ) { + return true; + } + + visited[i][j] = false; + return false; + } + + private boolean isVisited(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) { + return i < 0 || + j < 0 || + i >= board.length || + j >= board[0].length || + visited[i][j] || + board[i][j] != word.charAt(wordIndex); + } +} + From cd835e1ac1e76f703aa4e0c083ca5f130f592078 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Sat, 26 Apr 2025 00:32:49 +0900 Subject: [PATCH 5/5] coin change solution --- coin-change/Yn3-3xh.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 coin-change/Yn3-3xh.java diff --git a/coin-change/Yn3-3xh.java b/coin-change/Yn3-3xh.java new file mode 100644 index 000000000..f44e032ab --- /dev/null +++ b/coin-change/Yn3-3xh.java @@ -0,0 +1,29 @@ +/** +[문제풀이] +- 배열에 주어진 수를 더해서 amount 만들기 +- 배열은 중복 가능 +- amount를 만들 수 없으면 -1 +- amount를 만든 동전의 최소 개수 구하기 +- DP +time: O(N M), space: O(N) + +[회고] +DP로 풀면 되지 않을까 라는 짐작은 가는데, +아직 풀이방법이 부족하다.. +해결방법을 보고 겨우 이해는 했다.. + */ +class Solution { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, amount + 1); + dp[0] = 0; + + for (int coin : coins) { + for (int i = coin; i <= amount; i++) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + return dp[amount] > amount ? -1 : dp[amount]; + } +} +