Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions course-schedule/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

course-schedule/ICE0208.java
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<>();
        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;
    }
}
  • 패턴: BFS, Topological Sort
  • 설명: 입력 의존성 그래프를 만들고 위상 정렬 방식으로 처리하며, 큐를 이용해 차수 0 노드를 순차적으로 제거하는 구조로 패키지 의존성 해결에 적합한 BFS/위상 정렬 패턴이 사용됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(N + P)
Space O(N + P)

피드백: 그래프를 인접 리스트로 구성하고 진입 차수 배열로 위상정렬을 수행한다. 모든 정점을 방문하면 사이클이 없음을 뜻한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graph와 indegree를 초기화 하는 부분과 실제 판단하는 부분으로 크게 나뉘는데 어디까지가 초기화고 어디부터가 실제 판단 로직인지 바로 알기는 어려웠던 거 같습니다. 초기화하는 부분 정도만 함수로 빼거나 초기화/판단 로직 구분이 되도록 주석으로 나타내면 어떨까 싶네요~

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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<>();

@parkhojeong parkhojeong Aug 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
}
16 changes: 16 additions & 0 deletions invert-binary-tree/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

invert-binary-tree/ICE0208.java
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;
    }
}
  • 패턴: Recursive, Binary Search, DFS
  • 설명: 루트 트리를 재귀적으로 방문하여 좌우 자식을 교환하는 방식으로 이진 트리를 뒤집는다. 재귀 탐색(깊이 우선 탐색) 패턴과 단순한 재귀 응용으로 분류된다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 트리의 각 노드를 한 번씩 방문하고 자식 노드를 교환한다. 최악의 경우 깊이는 트리의 높이 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;
}
}
34 changes: 34 additions & 0 deletions search-in-rotated-sorted-array/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

search-in-rotated-sorted-array/ICE0208.java
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;
    }
}
  • 패턴: Binary Search
  • 설명: 로테이트된 정렬 배열에서 목표 값을 이진 탐색으로 찾되, 어느 쪽 절반이 정렬되었는지 판단하고 그에 맞춰 범위를 절단하는 패턴으로 구현됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(log n)
Space O(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;
}
}
Loading