-
-
Notifications
You must be signed in to change notification settings - Fork 361
[ICE0208] WEEK 10 Solutions #2843
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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. graph와 indegree를 초기화 하는 부분과 실제 판단하는 부분으로 크게 나뉘는데 어디까지가 초기화고 어디부터가 실제 판단 로직인지 바로 알기는 어려웠던 거 같습니다. 초기화하는 부분 정도만 함수로 빼거나 초기화/판단 로직 구분이 되도록 주석으로 나타내면 어떨까 싶네요~
Member
Author
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,48 @@ | ||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Queue; | ||
|
|
||
| class Solution { | ||
| public boolean canFinish(int numCourses, int[][] prerequisites) { | ||
| int[] indegree = new int[numCourses]; | ||
|
|
||
| List<List<Integer>> graph = new ArrayList<>(); | ||
|
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. graph 변수에 어떤 값이 들어가는지 관련 코드를 찾아 보고나서 이해가 되었던 거 같습니다. prerequisiteCourse 에 대한 nextCourses가 담기는데 의미를 나타내도록 네이밍을 하면 더 좋을 거 같습니다. |
||
| for (int course = 0; course < numCourses; course++) { | ||
| graph.add(new ArrayList<>()); | ||
| } | ||
|
|
||
| for (int[] prerequisite : prerequisites) { | ||
| int course = prerequisite[0]; | ||
| int prerequisiteCourse = prerequisite[1]; | ||
|
|
||
| graph.get(prerequisiteCourse).add(course); | ||
| indegree[course]++; | ||
| } | ||
|
|
||
| Queue<Integer> queue = new ArrayDeque<>(); | ||
|
|
||
| for (int course = 0; course < numCourses; course++) { | ||
| if (indegree[course] == 0) { | ||
| queue.offer(course); | ||
| } | ||
| } | ||
|
|
||
| int completedCourses = 0; | ||
|
|
||
| while (!queue.isEmpty()) { | ||
| int course = queue.poll(); | ||
| completedCourses++; | ||
|
|
||
| for (int nextCourse : graph.get(course)) { | ||
| indegree[nextCourse]--; | ||
|
|
||
| if (indegree[nextCourse] == 0) { | ||
| queue.offer(nextCourse); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return completedCourses == numCourses; | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석invert-binary-tree/ICE0208.javaclass Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
invertTree(root.left);
invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
📊 시간/공간 복잡도 분석
피드백: 트리의 각 노드를 한 번씩 방문하고 자식 노드를 교환한다. 최악의 경우 깊이는 트리의 높이 h에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution { | ||
| public TreeNode invertTree(TreeNode root) { | ||
| if (root == null) { | ||
| return null; | ||
| } | ||
|
|
||
| invertTree(root.left); | ||
| invertTree(root.right); | ||
|
|
||
| TreeNode temp = root.left; | ||
| root.left = root.right; | ||
| root.right = temp; | ||
|
|
||
| return root; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석search-in-rotated-sorted-array/ICE0208.javaclass Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
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;
}
}
📊 시간/공간 복잡도 분석
피드백: 왼쪽 또는 오른쪽 중 어느 절반이 정렬되었는지 판별하고, 정렬된 절반에서 타깃이 위치하는지 확인한 후 범위를 축소한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| class Solution { | ||
| public int search(int[] nums, int target) { | ||
| int left = 0; | ||
| int right = nums.length - 1; | ||
|
|
||
| while (left <= right) { | ||
| int mid = left + (right - left) / 2; | ||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석
course-schedule/ICE0208.java
📊 시간/공간 복잡도 분석
피드백: 그래프를 인접 리스트로 구성하고 진입 차수 배열로 위상정렬을 수행한다. 모든 정점을 방문하면 사이클이 없음을 뜻한다.
개선 제안: 현재 구현이 적절해 보입니다.