diff --git a/coin-change/YoungSeok-Choi.java b/coin-change/YoungSeok-Choi.java new file mode 100644 index 000000000..90d06c7bc --- /dev/null +++ b/coin-change/YoungSeok-Choi.java @@ -0,0 +1,17 @@ +import java.util.Arrays; + +class Solution { + 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]; + } +} diff --git a/find-minimum-in-rotated-sorted-array/YoungSeok-Choi.java b/find-minimum-in-rotated-sorted-array/YoungSeok-Choi.java new file mode 100644 index 000000000..c7fa8a528 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/YoungSeok-Choi.java @@ -0,0 +1,13 @@ +// NOTE: 문제에서 반드시 log n 복잡도의 알고리즘을 작성하라고 했는데.. 맞았다.. +// 이런걸 묻는건지... 다른 풀이 코드 보면서 복기가 필요함.. +class Solution { + public int findMin(int[] nums) { + int min = 9876521; + + for(int i = 0; i < nums.length; i++) { + min = Math.min(min, nums[i]); + } + + return min; + } +} diff --git a/maximum-depth-of-binary-tree/YoungSeok-Choi.java b/maximum-depth-of-binary-tree/YoungSeok-Choi.java new file mode 100644 index 000000000..77b892b48 --- /dev/null +++ b/maximum-depth-of-binary-tree/YoungSeok-Choi.java @@ -0,0 +1,35 @@ +import java.util.LinkedList; +import java.util.Queue; + +// 시간복잡도 O(n) +class Solution { + public int depth = 0; + public int maxDepth(TreeNode root) { + + if(root == null) { + return depth; + } + + Queue q = new LinkedList<>(); + q.add(root); + + while(!q.isEmpty()) { + int size = q.size(); + depth++; + + for(int i = 0; i < size; i++) { + TreeNode p = q.poll(); + + if(p.right != null) { + q.add(p.right); + } + + if(p.left != null) { + q.add(p.left); + } + } + } + + return depth; + } +} diff --git a/merge-two-sorted-lists/YoungSeok-Choi.java b/merge-two-sorted-lists/YoungSeok-Choi.java new file mode 100644 index 000000000..10164c9ef --- /dev/null +++ b/merge-two-sorted-lists/YoungSeok-Choi.java @@ -0,0 +1,44 @@ +// 시간복잡도 O(N + M) +class Solution { + + public ListNode root = null; + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + + while(list1 != null || list2 != null) { + int v1 = 9999; + int v2 = 9999; + + if(list1 != null) { + v1 = list1.val; + } + + if(list2 != null) { + v2 = list2.val; + } + + if(v1 < v2) { + addNode(v1); + list1 = list1.next; + } else { + addNode(v2); + list2 = list2.next; + } + } + + return root; + } + + public void addNode (int val) { + if(root == null) { + root = new ListNode(val); + return; + } + + ListNode now = root; + while(now.next != null) { + now = now.next; + } + + now.next = new ListNode(val); + } +} diff --git a/word-search/YoungSeok-Choi.java b/word-search/YoungSeok-Choi.java new file mode 100644 index 000000000..8b3cc120f --- /dev/null +++ b/word-search/YoungSeok-Choi.java @@ -0,0 +1,120 @@ +// NOTE: Queue를 처음에 써서 탐색하며 꼬여버리는 문제가 있었다.. +// NOTE: 원본 문자의 index를 사용해서 해결. + +import java.util.LinkedList; +import java.util.Queue; + +class Solution { + + public boolean[][] visit; + int w = 0; + int h = 0; + int[] dx = {1, 0, -1, 0}; + int[] dy = {0, 1, 0, -1}; + + public boolean exist(char[][] board, String word) { + + char[] cArr = word.toCharArray(); + w = board.length; + h = board[0].length; + visit = new boolean[w][h]; + + for(int i = 0; i < board.length; i++) { + for(int j = 0; j < board[0].length; j++) { + if(cArr[0] == board[i][j]) { + + if(dfs(board, word, i, j, 0)) { + return true; + } + } + } + } + + return false; + } + + public boolean dfs(char[][] b, String word, int x, int y, int idx) { + if(idx == word.length()) return true; + + if(x < 0 || x >= w || y < 0 || y >= h || b[x][y] != word.charAt(idx) || visit[x][y]) { + return false; + } + + visit[x][y] = true; + + for(int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if(dfs(b, word, nx, ny, idx + 1)) { + return true; + } + } + + visit[x][y] = false; + return false; + } + + + + class WrongSolution { + + public boolean[][] visit; + Queue q = new LinkedList(); + int w = 0; + int h = 0; + int[] dx = {1, 0, -1, 0}; + int[] dy = {0, 1, 0, -1}; + + public boolean exist(char[][] board, String word) { + + char[] cArr = word.toCharArray(); + w = board.length; + h = board[0].length; + visit = new boolean[w][h]; + + for(int i = 0; i < board.length; i++) { + for(int j = 0; j < board[0].length; j++) { + if(cArr[0] == board[i][j]) { + q = new LinkedList(); + visit = new boolean[w][h]; + for(char c : word.toCharArray()) { + q.add(c); + } + + + dfs(board, i, j); + if(q.isEmpty()) { + return true; + } + } + } + } + + return false; + } + + public void dfs(char[][] b, int x, int y) { + if(x < 0 || x >= w || y < 0 || y >= h || visit[x][y]) { + return; + } + + if(q.isEmpty()) { + return; + } + + if(b[x][y] != q.peek()) { + return; + } + + q.poll(); + visit[x][y] = true; + + for(int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + dfs(b, nx, ny); + } + } + }}