-
-
Notifications
You must be signed in to change notification settings - Fork 305
[se6816] WEEK 04 solutions #2135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36ed55b
1ef5b18
d13f273
af5c584
5f4f2e5
7fe3df4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문제 풀어야할 함수 밖에서 다른 함수를 선언한 것이 인상깊었습니다 그리고 별도의 호출 없이 그냥 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아예 정답용 연결리스트를 새로 만들었군요 기존의 제가 푸는 코딩테스트와는 다른 결의 문제 풀이를 보여주셔서 많은 배움을 얻고간다 생각합니다 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
|
|
||
|
Comment on lines
+43
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 if문 내부에 위 3조건을 한꺼번에 넣었었는데, 그냥 나눠서 |
||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그냥 for문을 통해 순회하셨군요
저는 이진탐색을 통해 풀긴했는데, 혹시 여유가 되신다면 이진탐색으로도 풀어보는 것은 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아, 먼저 죄송하단 말씀 먼저 드립니다. ZetBe님이 코멘트를 3일 전에 달아주신 것 같은데, 사유는 모르겠으나, 어제까지 저에게 보이지 않아서 뒤늦게 파악했습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 다른 분 리뷰하다 보니, 이분탐색으로 풀 수 있다는 것을 파악할 수 있었고, 배움을 얻을 수 있었습니다. 이진탐색으로 풀었던 코드 추가로 커밋하였습니다.