-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hyeri0903] WEEK10 Solutions #2585
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
base: main
Are you sure you want to change the base?
Changes from all commits
a9afd14
93fb400
7da0598
c6e1bcf
307a0e3
11d656c
63241cf
9e561e2
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,56 @@ | ||
| class Solution { | ||
| public boolean canFinish(int numCourses, int[][] prerequisites) { | ||
| /** | ||
| 1.문제: [a, b] = b를 수강해야 a 수강 가능, 모두 수강가능하면 true, 아니면 flase return | ||
| 2.constraints | ||
| - numCourses min = 1, max = 2000 | ||
| - prerequisites length min = 0, max = 5000 | ||
| 3.solution | ||
| - dfs, graph에 cycle 이 존재하는지 check. cycle 이 존재하면 수강 다 못하므로 false return | ||
| */ | ||
| List<List<Integer>> graph = new ArrayList<>(); | ||
| for(int i = 0; i < numCourses; i++) { | ||
| graph.add(new ArrayList<>()); | ||
| } | ||
|
|
||
| //graph 생성 | ||
| for(int i = 0; i < prerequisites.length; i++) { | ||
| int a = prerequisites[i][0]; | ||
| int b = prerequisites[i][1]; | ||
|
|
||
| graph.get(b).add(a); | ||
| } | ||
|
|
||
| //dfs cycle check | ||
| //state: 0 = 방문 전, 1 = 방문 중, 2 = 방문 완료 | ||
| int[] state = new int[numCourses]; | ||
| //node 개수(numCourses)만큼 순회 | ||
| for(int i = 0; i < numCourses; i++) { | ||
| //cycle 존재하면 false return | ||
| if(dfs(i, graph, state)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| boolean dfs(int i, List<List<Integer>> graph, int[] state) { | ||
| //순회 cycle 발견하면 true | ||
| if(state[i] == 1) { | ||
| return true; | ||
| } | ||
| if(state[i] == 2) { | ||
| return false; | ||
| } | ||
| state[i] = 1; | ||
|
|
||
| for(int next: graph.get(i)) { | ||
| if(dfs(next, graph, state)) { | ||
| return true; | ||
| } | ||
| } | ||
| state[i] = 2; | ||
| return false; | ||
| } | ||
| } |
|
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,52 @@ | ||
| /** | ||
| * 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 TreeNode invertTree(TreeNode root) { | ||
| /** | ||
| 1.문제: inverted binary tree 출력 | ||
| 2.constraints: node 개수 min = 0, max = 100 | ||
| 3.solution: left, right node swap | ||
| time complexity: | ||
| - BST인 경우 best case : O(log n) | ||
| - skwed 인 경우 worst case: O(n) | ||
| - space complexity: O(h) | ||
| */ | ||
|
|
||
| if(root == null) { | ||
| return null; | ||
| } | ||
|
|
||
| dfs(root); | ||
|
|
||
| return root; | ||
|
|
||
| } | ||
|
|
||
| void dfs(TreeNode root) { | ||
| if(root == null) { | ||
| return; | ||
| } | ||
| //swap | ||
| TreeNode tmp = root.left; | ||
| root.left = root.right; | ||
| root.right = tmp; | ||
|
|
||
| //left recursion | ||
| dfs(root.left); | ||
| //right recursion | ||
| dfs(root.right); | ||
| } | ||
| } |
|
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,26 @@ | ||
| class Solution { | ||
| public boolean canJump(int[] nums) { | ||
| /** | ||
| 1.문제: last index 에 도달 가능하면 true, 아니면 false return | ||
| 2.constraints | ||
| - first index 에서 시작 | ||
| - nums.length min = 1, max = 10000 | ||
| - value min = 0, max = 100000 | ||
| 3.solution | ||
| - brute force: 0 index 부터 1, 2, 3번째 등등 시도, time: O(n^2) | ||
| - 가장 먼 index 을 업데이트하면서 체크 : time O(n) | ||
| */ | ||
| int n = nums.length; | ||
| int farthest = 0; | ||
|
|
||
| for(int i = 0; i < n; i++) { | ||
| //현재까지 도달 가능한지 체크 | ||
| if(i > farthest) { | ||
| return false; | ||
| } | ||
| //도달가능한 가장 먼 인덱스 update | ||
| farthest = Math.max(farthest, i + nums[i]); | ||
| } | ||
| return true; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 노드를 힙에 넣고 하나씩 꺼내며 병합하는 방식으로, 노드 수 N과 리스트 수 k에 비례하는 시간과 공간 복잡도를 갖는다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * 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 mergeKLists(ListNode[] lists) { | ||
| /** | ||
| 1.문제: ascending liknked list 를 모두 sorted list 로 머지해라. | ||
| 2.조건 | ||
| - k = list 길이, 최소 = 0, 최대 = 10^4 | ||
| - 원소값 0 이상, 500 이하 | ||
| 3.풀이 | ||
| - priority queue(min-heap): time = O(n log k), space = O(k) | ||
| - 각 list 를 min heap 에 넣고 하나씩 뽑아서 새 리스트에 연결. | ||
| - Heap 에 들어가고 나올때마다 O(log k), 총 노드 수 N = O(N logk) | ||
| - Heap 사용 -> space = k | ||
| */ | ||
|
|
||
| if(lists == null || lists.length == 0) return null; | ||
|
|
||
| PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val); | ||
|
|
||
| //all list의 첫 노드 넣기 | ||
| for(ListNode node: lists) { | ||
| if (node != null) { | ||
| pq.offer(node); | ||
| } | ||
| } | ||
|
|
||
| //결과 리스트를 위한 더미 노드 | ||
| ListNode dummy = new ListNode(0); | ||
| ListNode curr = dummy; | ||
|
|
||
| while(!pq.isEmpty()) { | ||
| ListNode node = pq.poll(); //최소값 pop | ||
| curr.next = node; //결과 리스트에 연결 | ||
| curr = curr.next; //포인터 이동 | ||
| //다음 노드를 heap에 insert | ||
| if(node.next != null) { | ||
| pq.offer(node.next); | ||
| } | ||
| } | ||
| return dummy.next; | ||
| } | ||
| } |
|
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,44 @@ | ||
| class Solution { | ||
| public int search(int[] nums, int target) { | ||
| /** | ||
| 1.prob: index k기준으로 left rotated 된 array 에서 target index return, 존재하지 않으면 -1 return | ||
| 2.constraints | ||
| - asc 정렬된 배열, 모두 unique 한 값 | ||
| - 반드시 O(log n) 으로 풀 것 | ||
| - num.length min=1, max = 5000 | ||
| 3.solution | ||
| - bruteforce, for문 -> time: O(n) | ||
| - binary search -> time: O(log n), space: O(1) | ||
| */ | ||
|
|
||
| int n = nums.length; | ||
| int left = 0, right = n - 1; | ||
|
|
||
|
|
||
| while(left <= right) { | ||
| int mid = (left + right) / 2; | ||
|
|
||
| //target 찾으면 index return | ||
| if(nums[mid] == target) { | ||
| return mid; | ||
| } | ||
|
|
||
| if(nums[left] <= nums[mid]) { | ||
| //왼쪽이 정렬된 경우 | ||
| if(nums[left] <= target && target < nums[mid]) { | ||
| right = mid - 1; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } else { | ||
| //오른쪽이 정렬된 경우 | ||
| if(nums[mid] < target && target <= nums[right]) { | ||
| left = mid + 1; | ||
| } else { | ||
| right = mid - 1; | ||
| } | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 그래프를 인접 리스트로 표현하고, DFS를 통해 사이클을 탐지하는 방식으로 정점과 간선 수에 비례하는 시간과 공간 복잡도를 갖는다.
개선 제안: 현재 구현이 적절해 보입니다.