-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_210.java
88 lines (85 loc) · 3.04 KB
/
_210.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.fishercoder.solutions.firstthousand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class _210 {
public static class Solution1 {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
for (int[] pre : prerequisites) {
List<Integer> list = adjacencyList.getOrDefault(pre[1], new ArrayList<>());
list.add(pre[0]);
adjacencyList.put(pre[1], list);
indegree[pre[0]]++;
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
q.offer(i);
}
}
int[] order = new int[numCourses];
int i = 0;
while (!q.isEmpty()) {
Integer course = q.poll();
order[i++] = course;
if (adjacencyList.containsKey(course)) {
for (int neighbor : adjacencyList.get(course)) {
indegree[neighbor]--;
if (indegree[neighbor] == 0) {
q.offer(neighbor);
}
}
}
}
if (i == numCourses) {
return order;
}
return new int[] {};
}
}
public static class Solution2 {
/*
* Instead of using a map, we can use an array of list type, it turned out to be even faster on LeetCode.
*/
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer>[] adjList = new ArrayList[numCourses];
for (int i = 0; i < numCourses; i++) {
adjList[i] = new ArrayList<>();
}
int[] indegree = new int[numCourses];
for (int[] pre : prerequisites) {
indegree[pre[0]]++;
adjList[pre[1]].add(pre[0]);
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < indegree.length; i++) {
if (indegree[i] == 0) {
q.offer(i);
}
}
int index = 0;
int[] order = new int[numCourses];
while (!q.isEmpty()) {
Integer curr = q.poll();
order[index++] = curr;
// NOTE: we only need to go through adjList[curr] here now, instead of going through
// all prerequisites again now.
for (int v : adjList[curr]) {
indegree[v]--;
if (indegree[v] == 0) {
q.offer(v);
}
}
}
if (index == numCourses) {
return order;
}
return new int[] {};
}
}
}