Skip to content

Commit 0b57d02

Browse files
update 207
1 parent 1d82f14 commit 0b57d02

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_207.java

+33-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@
88
import java.util.Queue;
99
import java.util.Set;
1010

11+
/**
12+
* 207. Course Schedule
13+
* <p>
14+
* There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
15+
* You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
16+
* For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
17+
* Return true if you can finish all courses. Otherwise, return false.
18+
* <p>
19+
* Example 1:
20+
* Input: numCourses = 2, prerequisites = [[1,0]]
21+
* Output: true
22+
* Explanation: There are a total of 2 courses to take.
23+
* To take course 1 you should have finished course 0. So it is possible.
24+
* <p>
25+
* Example 2:
26+
* Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
27+
* Output: false
28+
* Explanation: There are a total of 2 courses to take.
29+
* To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
30+
* <p>
31+
* Constraints:
32+
* 1 <= numCourses <= 2000
33+
* 0 <= prerequisites.length <= 5000
34+
* prerequisites[i].length == 2
35+
* 0 <= ai, bi < numCourses
36+
* All the pairs prerequisites[i] are unique.
37+
*/
1138
public class _207 {
1239

1340
public static class Solution1 {
@@ -33,11 +60,11 @@ public boolean canFinish(int numCourses, int[][] prerequisites) {
3360
Iterator<Integer> it = zeroDegree.iterator();
3461
int course = it.next();
3562
zeroDegree.remove(course);
36-
for (int[] prereq : prerequisites) {
37-
if (prereq[1] == course) {
38-
indegree[prereq[0]]--;
39-
if (indegree[prereq[0]] == 0) {
40-
zeroDegree.add(prereq[0]);
63+
for (int[] pre : prerequisites) {
64+
if (pre[1] == course) {
65+
indegree[pre[0]]--;
66+
if (indegree[pre[0]] == 0) {
67+
zeroDegree.add(pre[0]);
4168
}
4269
}
4370
}
@@ -93,7 +120,7 @@ public boolean canFinish(int numCourses, int[][] prerequisites) {
93120
public static class Solution3 {
94121
/**
95122
* DFS, the fastest method in all, with the help of a cache and also converted edges into adjacency list,
96-
* although theoretically, all these three methods' time complexity is: O(V+E)
123+
* although theoretically, all these three methods' time complexity are: O(V+E)
97124
*/
98125
public boolean canFinish(int numCourses, int[][] prerequisites) {
99126
List<List<Integer>> courseList = new ArrayList<>();

Diff for: src/test/java/com/fishercoder/_207Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._207;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static junit.framework.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _207Test {
1010
private static _207.Solution1 solution1;
@@ -13,8 +13,8 @@ public class _207Test {
1313
private static int[][] prerequisites;
1414
private static int numCourses;
1515

16-
@BeforeClass
17-
public static void setup() {
16+
@BeforeEach
17+
public void setup() {
1818
solution1 = new _207.Solution1();
1919
solution2 = new _207.Solution2();
2020
solution3 = new _207.Solution3();

0 commit comments

Comments
 (0)