Skip to content

Commit 677f6e3

Browse files
committedMar 20, 2025
DaleStudy#261 Course Schedule
1 parent 4db2ab0 commit 677f6e3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 

‎course-schedule/forest000014.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
위상 정렬을 사용해서 풀었습니다.
5+
*/
6+
7+
class Solution {
8+
public boolean canFinish(int numCourses, int[][] prerequisites) {
9+
int[] inDegree = new int[numCourses];
10+
Queue<Integer> queue = new LinkedList<>();
11+
List<List<Integer>> graph = new ArrayList<>();
12+
for (int i = 0; i < numCourses; i++) {
13+
graph.add(new ArrayList<>());
14+
}
15+
16+
for (int i = 0; i < prerequisites.length; i++) {
17+
graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
18+
inDegree[prerequisites[i][0]]++;
19+
}
20+
21+
for (int i = 0; i < numCourses; i++) {
22+
if (inDegree[i] == 0) queue.offer(i);
23+
}
24+
25+
while (!queue.isEmpty()) {
26+
int curr = queue.poll();
27+
28+
for (int next : graph.get(curr)) {
29+
inDegree[next]--;
30+
if (inDegree[next] == 0) queue.offer(next);
31+
}
32+
}
33+
34+
for (int i = 0; i < numCourses; i++) {
35+
if (inDegree[i] > 0) return false;
36+
}
37+
return true;
38+
}
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.