Skip to content

Commit a2b9546

Browse files
add a solution for 1136
1 parent 4c4675c commit a2b9546

File tree

3 files changed

+86
-6
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+86
-6
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@
398398
| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java) | | Medium | String, DP
399399
| 1138 | [Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java) | [:tv:](https://youtu.be/rk-aB4rEOyU) | Medium | HashTable, String |
400400
| 1137 | [N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java) | | Easy ||
401-
| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium ||
401+
| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium |Topological Sort
402402
| 1134 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java) | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4) | Easy ||
403403
| 1133 | [Largest Unique Number](https://leetcode.com/problems/largest-unique-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java) | [:tv:](https://youtu.be/Fecpt1YZlCs) | Easy ||
404404
| 1128 | [Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java) | [:tv:](https://www.youtube.com/watch?v=7EpEEHAAxyw) | Easy ||

Diff for: src/main/java/com/fishercoder/solutions/secondthousand/_1136.java

+45
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,49 @@ public int minimumSemesters(int n, int[][] relations) {
4949
return taken.size() != n ? -1 : minSemesters;
5050
}
5151
}
52+
53+
public static class Solution2 {
54+
/**
55+
* A straightforward one to practice topological sort (template).
56+
* Use an indegree/outdegree array and an array of list type.
57+
*/
58+
59+
public int minimumSemesters(int n, int[][] relations) {
60+
List<Integer>[] adjList = new ArrayList[n + 1];
61+
for (int i = 1; i <= n; i++) {
62+
adjList[i] = new ArrayList<>();
63+
}
64+
int[] indegree = new int[n + 1];
65+
for (int[] rel : relations) {
66+
indegree[rel[1]]++;
67+
adjList[rel[0]].add(rel[1]);
68+
}
69+
Queue<Integer> q = new LinkedList<>();
70+
for (int i = 1; i <= n; i++) {
71+
if (indegree[i] == 0) {
72+
q.offer(i);
73+
}
74+
}
75+
int semesters = 0;
76+
while (!q.isEmpty()) {
77+
int size = q.size();
78+
for (int i = 0; i < size; i++) {
79+
Integer curr = q.poll();
80+
for (int v : adjList[curr]) {
81+
indegree[v]--;
82+
if (indegree[v] == 0) {
83+
q.offer(v);
84+
}
85+
}
86+
}
87+
semesters++;
88+
}
89+
for (int i = 1; i <= n; i++) {
90+
if (indegree[i] != 0) {
91+
return -1;
92+
}
93+
}
94+
return semesters;
95+
}
96+
}
5297
}

Diff for: src/test/java/com/fishercoder/secondthousand/_1136Test.java

+40-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22

33
import com.fishercoder.common.utils.CommonUtils;
44
import com.fishercoder.solutions.secondthousand._1136;
5-
import org.junit.BeforeClass;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
77

8-
import static org.junit.Assert.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
public class _1136Test {
1111
private static _1136.Solution1 solution1;
12+
private static _1136.Solution2 solution2;
1213

13-
@BeforeClass
14-
public static void setup() {
14+
@BeforeEach
15+
public void setup() {
1516
solution1 = new _1136.Solution1();
17+
solution2 = new _1136.Solution2();
1618
}
1719

1820
@Test
1921
public void test1() {
2022
assertEquals(2, solution1.minimumSemesters(3, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3],[2,3]")));
23+
assertEquals(2, solution2.minimumSemesters(3, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3],[2,3]")));
2124
}
2225

2326
@Test
2427
public void test2() {
2528
assertEquals(-1, solution1.minimumSemesters(3, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[2,3],[3,1]")));
29+
assertEquals(-1, solution2.minimumSemesters(3, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[2,3],[3,1]")));
2630
}
2731

2832
@Test
@@ -57,6 +61,37 @@ public void test3() {
5761
+ "[15,22],[10,14],[3,9],[13,20],[1,10],[9,21],[10,25],[9,24],[14,20],[9,25],[8,13],[7,12],"
5862
+ "[5,13],[6,10],[2,5],[2,18],[14,19],[1,11],[7,22],[18,25],[11,19],"
5963
+ "[18,19],[4,18],[17,18],[2,11]")));
64+
65+
assertEquals(25, solution2.minimumSemesters(25, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(""
66+
+ "[5,10],[11,14],[21,22],[16,19],[21,25],[6,18],[1,9],[4,7],"
67+
+ "[10,23],[5,14],[9,18],[18,21],[11,22],[1,15],[1,2],[5,18],[7,20],[2,23],"
68+
+ "[12,13],[9,14],[10,16],[11,21],[5,12],[2,24],[8,17],[15,17],[10,13],[11,16],"
69+
+ "[20,22],[7,11],[9,15],[16,22],[18,20],[19,22],[10,18],[3,20],[16,25],[10,15],"
70+
+ "[1,23],[13,16],[23,25],[1,8],[4,10],[19,24],[11,20],[3,18],[6,25],[11,13],"
71+
+ "[13,15],[22,24],[6,24],[17,20],[2,25],[15,24],[8,21],[14,16],[5,16],[19,23],"
72+
+ "[1,5],[4,22],[19,20],[12,15],[16,18],[9,13],[13,22],[14,22],[2,8],[3,13],"
73+
+ "[9,23],[14,15],[14,17],[8,20],[9,17],[3,19],[8,25],[2,12],[7,24],[19,25],"
74+
+ "[1,13],[6,11],[14,21],[7,15],[3,14],[15,23],[10,17],[4,20],[6,14],[10,21],"
75+
+ "[2,13],[3,21],[8,11],[5,21],[6,23],[17,25],[16,21],[12,22],[1,16],"
76+
+ "[6,19],[7,25],[3,23],[11,25],[3,10],[6,7],[2,3],[5,25],[1,6],[4,17],"
77+
+ "[2,16],[13,17],[17,22],[6,13],[5,6],[4,11],[4,23],[4,8],[12,23],[7,21],"
78+
+ "[5,20],[3,24],[2,10],[13,14],[11,24],[1,3],[2,7],[7,23],[6,17],[5,17],"
79+
+ "[16,17],[8,15],[8,23],[7,17],[14,18],[16,23],[23,24],[4,12],[17,19],[5,9],"
80+
+ "[10,11],[5,23],[2,9],[1,19],[2,19],[12,20],[2,14],[11,12],[1,12],[13,23],[4,9],"
81+
+ "[7,13],[15,20],[21,24],[8,18],[9,11],[8,19],[6,22],[16,20],[22,25],[20,21],[6,16],"
82+
+ "[3,17],[1,22],[9,22],[20,24],[2,6],[9,16],[2,4],[2,20],[20,25],[9,10],[3,11],[15,18],"
83+
+ "[1,20],[3,6],[8,14],[10,22],[12,21],[7,8],[8,16],[9,20],[3,8],[15,21],[17,21],[11,18],"
84+
+ "[13,24],[17,24],[6,20],[4,15],[6,15],[3,22],[13,21],[2,22],[13,25],[9,12],[4,19],[1,24],"
85+
+ "[12,19],[5,8],[1,7],[3,16],[3,5],[12,24],[3,12],[2,17],[18,22],[4,25],[8,24],"
86+
+ "[15,19],[18,23],[1,4],[1,21],[10,24],[20,23],[4,14],[16,24],[10,20],[18,24],"
87+
+ "[1,14],[12,14],[10,12],[4,16],[5,19],[4,5],[19,21],[15,25],[1,18],[2,21],[4,24],"
88+
+ "[7,14],[4,6],[15,16],[3,7],[21,23],[1,17],[12,16],[13,18],[5,7],[9,19],[2,15],[22,23],"
89+
+ "[7,19],[17,23],[8,22],[11,17],[7,16],[8,9],[6,21],[4,21],[4,13],[14,24],[3,4],[7,18],"
90+
+ "[11,15],[5,11],[12,17],[6,9],[1,25],[12,18],[6,12],[8,10],[6,8],[11,23],[7,10],[14,25],"
91+
+ "[14,23],[12,25],[5,24],[10,19],[3,25],[7,9],[8,12],[5,22],[24,25],[13,19],[3,15],[5,15],"
92+
+ "[15,22],[10,14],[3,9],[13,20],[1,10],[9,21],[10,25],[9,24],[14,20],[9,25],[8,13],[7,12],"
93+
+ "[5,13],[6,10],[2,5],[2,18],[14,19],[1,11],[7,22],[18,25],[11,19],"
94+
+ "[18,19],[4,18],[17,18],[2,11]")));
6095
}
6196

6297
}

0 commit comments

Comments
 (0)