diff --git a/coin-change/jinhyungrhee.java b/coin-change/jinhyungrhee.java new file mode 100644 index 000000000..e184c1795 --- /dev/null +++ b/coin-change/jinhyungrhee.java @@ -0,0 +1,22 @@ +import java.util.*; +class Solution { + int INF = 987654321; + public int coinChange(int[] coins, int amount) { + if (amount == 0) return 0; + + int[] dp = new int[amount + 1]; + Arrays.fill(dp, INF); + + dp[0] = 0; + for (int coin : coins) { + if (coin <= amount) dp[coin] = 1; + } + + for (int i = 1; i <= amount; i++) { + for (int coin : coins) { + if ((i - coin) >= 0) dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + return (dp[amount] == INF) ? -1 : dp[amount]; + } +} diff --git a/find-minimum-in-rotated-sorted-array/jinhyungrhee.java b/find-minimum-in-rotated-sorted-array/jinhyungrhee.java new file mode 100644 index 000000000..3ef361726 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/jinhyungrhee.java @@ -0,0 +1,17 @@ +class Solution { + public int findMin(int[] nums) { + int left = 0, 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]; + } +} diff --git a/maximum-depth-of-binary-tree/jinhyungrhee.java b/maximum-depth-of-binary-tree/jinhyungrhee.java new file mode 100644 index 000000000..ac7a7bf48 --- /dev/null +++ b/maximum-depth-of-binary-tree/jinhyungrhee.java @@ -0,0 +1,17 @@ +class Solution { + public int maxVal; + public int maxDepth(TreeNode root) { + dfs(root, 0); + return maxVal; + } + public void dfs(TreeNode node, int step) { + + if (node == null) { + if (maxVal < step) maxVal = step; + return; + } + + dfs(node.left, step + 1); + dfs(node.right, step + 1); + } +} diff --git a/merge-two-sorted-lists/jinhyungrhee.java b/merge-two-sorted-lists/jinhyungrhee.java new file mode 100644 index 000000000..555eabcae --- /dev/null +++ b/merge-two-sorted-lists/jinhyungrhee.java @@ -0,0 +1,29 @@ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + + ListNode dummy = new ListNode(0); + ListNode current = dummy; // ***실제로 노드를 이어나갈 포인터*** + + 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; + } + + // 남아있는 노드들 이어붙이기 + if (list1 != null) { + current.next = list1; + } + if (list2 != null) { + current.next = list2; + } + + return dummy.next; + } +} diff --git a/word-search/jinhyungrhee.java b/word-search/jinhyungrhee.java new file mode 100644 index 000000000..938a2f76e --- /dev/null +++ b/word-search/jinhyungrhee.java @@ -0,0 +1,40 @@ +class Solution { + public boolean exist(char[][] board, String word) { + + boolean[][] visited = new boolean[board.length][board[0].length]; + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if(dfs(board, word, visited, i, j, 0)) { + return true; + } + } + } + return false; + } + + public boolean dfs(char[][] board, String word, boolean[][] visited, int x, int y, int index) { + + // [성공] : 모든 글자 매칭 성공 + if (index == word.length()) return true; + + // [실패] : 범위 초과, 방문함, 글자 불일치 + if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || + visited[x][y] || board[x][y] != word.charAt(index)) { + return false; + } + + visited[x][y] = true; + int[][] dir = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; + for (int i = 0; i < 4; i++) { + int newX = x + dir[i][0], newY = y + dir[i][1]; + if (dfs(board, word, visited, newX, newY, index + 1)) { + return true; // 찾았으면 바로 리턴 + } + } + visited[x][y] = false; // backtrack + + return false; // 4방향 다 실패하면 false + + } +}