diff --git a/coin-change/se6816.java b/coin-change/se6816.java new file mode 100644 index 0000000000..98dc9aa2ec --- /dev/null +++ b/coin-change/se6816.java @@ -0,0 +1,74 @@ +/** + 내림차순 정렬 후, DFS를 통한 탐색 방식 + coins 길이 -> N + amount 값 -> M + 시간 복잡도 : O(N^M) -> 시간 초과 + 공간 복잡도 : O(M) + +*/ +class Solution2 { + public int coinChange(int[] coins, int amount) { + Arrays.sort(coins); + for (int i = 0; i < coins.length / 2; i++) { + int temp = coins[i]; + coins[i] = coins[coins.length-1-i]; + coins[coins.length-1-i] = temp; + } + return searchCoin(coins, 0, 0, amount, 0); + } + + public int searchCoin(int[] coins, int idx, int result, int target, int count) { + if(result == target) { + return count; + } + int result2 = Integer.MAX_VALUE; + for(int i = idx; i < coins.length; i++) { + int temp = result + coins[i]; + if(temp > target) { + continue; + } + int r = searchCoin(coins, i, temp, target, count+1); + if(r != -1) { + result2 = Math.min(result2, r); + } + } + + if(result2 == Integer.MAX_VALUE) { + result2 = -1; + } + + return result2; + } +} + +/** + dp를 이용하여, 최저 값을 구하는 방식 + coins의 길이 -> N + amount의 값 -> M + 시간 복잡도 : O(N*M) + 공간 복잡도 : O(M) +*/ +class Solution { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + + for (int coin : coins) { + for (int x = 0; x < amount; x++) { + if(dp[x] == Integer.MAX_VALUE) { + continue; + } + if((x + coin) > amount) { + break; + } + + dp[x + coin] = Math.min(dp[x + coin], dp[x] + 1); + + } + } + + return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount]; + } + +} diff --git a/find-minimum-in-rotated-sorted-array/se6816.java b/find-minimum-in-rotated-sorted-array/se6816.java new file mode 100644 index 0000000000..10e3f4e116 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/se6816.java @@ -0,0 +1,42 @@ +/** + for문 순회를 통해 최소값 찾기 + nums의 길이 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(1) + */ +class Solution { + public int findMin(int[] nums) { + int result = Integer.MAX_VALUE; + for(int num : nums) { + result = Math.min(result, num); + } + return result; + } +} + +/** + 이분 탐색을 통해, 최소 요소를 구하는 방식 + nums의 길이 -> N + 시간 복잡도 : O(logN) + 공간 복잡도 : O(1) +*/ +class Solution { + public int findMin(int[] nums) { + return nums[binarySearch(nums)]; + } + + public int binarySearch(int[] nums) { + int start = 0; + int end = nums.length -1; + while(start < end) { + int mid = (start + end) / 2; + if(nums[mid] < nums[end]) { + end = mid; + } else { + start = mid + 1; + } + } + return start; + } +} + diff --git a/maximum-depth-of-binary-tree/se6816.java b/maximum-depth-of-binary-tree/se6816.java new file mode 100644 index 0000000000..fe4fe63d3f --- /dev/null +++ b/maximum-depth-of-binary-tree/se6816.java @@ -0,0 +1,19 @@ +/** + DFS를 이용하여, 최대 깊이를 구하는 방식 + 트리 노드의 개수 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(log N) + */ +class Solution { + public int maxDepth(TreeNode root) { + return calculateDepth(root, 0); + } + + public int calculateDepth(TreeNode node, int depth) { + if(node == null) { + return depth; + } + + return Math.max(calculateDepth(node.left, depth + 1), calculateDepth(node.right, depth + 1)); + } +} diff --git a/merge-two-sorted-lists/se6816.java b/merge-two-sorted-lists/se6816.java new file mode 100644 index 0000000000..7846b4b948 --- /dev/null +++ b/merge-two-sorted-lists/se6816.java @@ -0,0 +1,51 @@ +/** + 연결 리스트를 활용하여, 하나의 리스트를 만드는 방식 + 시간 복잡도 : O(N+M) + 공간 복잡도 : O(1) + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + NodeList list=new NodeList(); + ListNode temp = null; + while(list1 != null && list2 != null){ + if(list1.val >= list2.val){ + list2 = merge(list, list2); + }else{ + list1 = merge(list, list1); + } + } + while(list1 !=null){ + list1 = merge(list, list1); + } + while(list2 != null){ + list2 = merge(list, list2); + } + return list.head; + } + public class NodeList { + ListNode head = null; + ListNode tail = null; + public NodeList(){ + } + + public void add(ListNode tempNode){ + if(head == null){ + head = tempNode; + tail = head; + }else{ + tail.next = tempNode; + tail = tail.next; + } + } + + } + + public ListNode merge(NodeList list, ListNode target) { + ListNode tempNode = null; + tempNode = target; + list.add(target); + target = target.next; + tempNode.next = null; + return target; + } +} diff --git a/word-search/se6816.java b/word-search/se6816.java new file mode 100644 index 0000000000..a54ee6f3a6 --- /dev/null +++ b/word-search/se6816.java @@ -0,0 +1,73 @@ + +/** + DFS를 통해 이중 배열에서 문자를 찾는 방식 + board의 길이 -> M + board[i]의 길이 -> N + 시간 복잡도 : O(M*N) + 공간 복잡도 : O(M*N) + */ +class Solution { + public static int[] moveX = {0, -1, 1, 0}; + public static int[] moveY = {-1, 0, 0, 1}; + public int N; + public int M; + public boolean exist(char[][] board, String word) { + M = board.length; + N = board[0].length; + boolean result = false; + char startCh = word.charAt(0); + for(int i = 0; i < M; i++) { + for(int j = 0; j < N; j++) { + if(board[i][j] != startCh) { + continue; + } + boolean[][] visited = new boolean[M][N]; + visited[i][j] = true; + result = result || search(board, visited, i, j, 1, word); + } + } + return result; + } + + public boolean search(char[][] board, boolean[][] visited, int x, int y, int len, String target) { + if(len >= target.length()) { + return true; + } + + boolean result = false; + + for(int i = 0; i < 4; i++) { + int tempX = x + moveX[i]; + int tempY = y + moveY[i]; + + if(outOfIndex(tempX, tempY)) { + continue; + } + + if(visited[tempX][tempY]) { + continue; + } + + if(board[tempX][tempY] != target.charAt(len)) { + continue; + } + + visited[tempX][tempY] = true; + result = search(board, visited, tempX, tempY, len + 1, target) || result; + if(result) { + break; + } + visited[tempX][tempY] = false; + } + + return result; + } + + public boolean outOfIndex(int x, int y){ + if(x < 0 || x >= M || y < 0 || y >= N) { + return true; + } + + return false; + } +}