Skip to content

Commit 2c94dc2

Browse files
committed
add 210 java, progress
1 parent a45ae6d commit 2c94dc2

File tree

6 files changed

+348
-22
lines changed

6 files changed

+348
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@
918918
130| [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [Python](./leetcode_python/Breadth-First-Search/surrounded-regions.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java) | _O(m + n)_ | | Medium |`bfs`, `dfs`,`union find`,good basic, `amazon`| AGAIN*********** (5)
919919
133| [Clone Graph](https://leetcode.com/problems/clone-graph/)| [Python](./leetcode_python/Breadth-First-Search/clone-graph.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java) | _O(n)_ | _O(n)_ | Medium |Curated Top 75, good trick, `check #138 Copy List with Random Pointer `,`graph`,`dfs`,`bfs`, `UBER`, `google`,`amazon`,`fb`| AGAIN**************** (10) (MUST)
920920
207| [Course Schedule](https://leetcode.com/problems/course-schedule/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Curated Top 75, Topological Sort, backtrack, `good trick`,`dfs`, `bfs` , `amazon`,`fb`| AGAIN**************** (13) (MUST)
921-
210| [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule-ii.py) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Topological Sort,check `# 207 Course Schedule ` first, `dfs`, `bfs` ,`amazon` ,`fb` | AGAIN********* (9) (again)
921+
210| [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule2.java) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Topological Sort,check `# 207 Course Schedule ` first, `dfs`, `bfs` ,`amazon` ,`fb` | AGAIN********* (10) (again)
922922
261| [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Python](./leetcode_python/Breadth-First-Search/graph-valid-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/GraphValidTree.java) | _O(\|V\| + \|E\|)_ | _O(\|V\| + \|E\|)_ | Medium | Curated Top 75, AGAIN, bfs, dfs, grpah, 🔒, `graph`,`quick union`,`union find` ,`google`,`amazon`,`fb`| AGAIN************* (11)(MUST)
923923
286| [Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Python](./leetcode_python/Breadth-First-Search/walls-and-gates.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/WallsAndGates.java)| _O(m * n)_ | _O(g)_ | Medium | 🔒, `dfs`, `bfs` , `good basic`, `google`,`amazon`,`FB`| AGAIN******** (7)
924924
310| [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)| [Python](./leetcode_python/Breadth-First-Search/minimum-height-trees.py) | _O(n)_ | _O(n)_ | Medium |`complex`| AGAIN (3) (not start)

data/progress.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
20241110: 53
2-
20241109: 207,210(todo),444
1+
20241110: 53,210
2+
20241109: 207,444
33
20241108: 1188,130,855(again)
44
20241104: 207
55
20241103: 900

data/to_review.txt

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
2025-01-03 -> ['207,210(todo),444']
1+
2025-01-04 -> ['53,210']
2+
2025-01-03 -> ['207,444']
23
2025-01-02 -> ['1188,130,855(again)']
34
2024-12-28 -> ['900']
45
2024-12-27 -> ['26,27', '802,1197,26']
56
2024-12-21 -> ['855,846']
67
2024-12-20 -> ['932']
78
2024-12-18 -> ['951,792']
8-
2024-12-14 -> ['163,1048']
9-
2024-12-13 -> ['207,210(todo),444', '298,729']
9+
2024-12-14 -> ['53,210', '163,1048']
10+
2024-12-13 -> ['207,444', '298,729']
1011
2024-12-12 -> ['1188,130,855(again)', '1146']
1112
2024-12-08 -> ['737']
1213
2024-12-07 -> ['900', '686,734,737']
1314
2024-12-06 -> ['26,27', '802,1197,26', '353']
1415
2024-12-05 -> ['528,334']
1516
2024-12-03 -> ['1145']
16-
2024-11-30 -> ['207,210(todo),444', '855,846', '1145,1219']
17+
2024-12-01 -> ['53,210']
18+
2024-11-30 -> ['207,444', '855,846', '1145,1219']
1719
2024-11-29 -> ['1188,130,855(again)', '932']
1820
2024-11-27 -> ['951,792', '524,221,889']
1921
2024-11-26 -> ['743,889']
2022
2024-11-25 -> ['837']
2123
2024-11-24 -> ['900']
22-
2024-11-23 -> ['26,27', '802,1197,26', '163,1048', '981']
23-
2024-11-22 -> ['207,210(todo),444', '298,729', '1087']
24+
2024-11-23 -> ['53,210', '26,27', '802,1197,26', '163,1048', '981']
25+
2024-11-22 -> ['207,444', '298,729', '1087']
2426
2024-11-21 -> ['1188,130,855(again)', '1146']
2527
2024-11-20 -> ['939']
26-
2024-11-18 -> ['430']
27-
2024-11-17 -> ['207,210(todo),444', '855,846', '737', '363']
28+
2024-11-18 -> ['53,210', '430']
29+
2024-11-17 -> ['207,444', '855,846', '737', '363']
2830
2024-11-16 -> ['1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
29-
2024-11-15 -> ['26,27', '802,1197,26', '353', '947']
30-
2024-11-14 -> ['207,210(todo),444', '951,792', '528,334']
31-
2024-11-13 -> ['1188,130,855(again)']
32-
2024-11-12 -> ['207,210(todo),444', '1145', '753']
33-
2024-11-11 -> ['207,210(todo),444', '1188,130,855(again)', '900', '727']
34-
2024-11-10 -> ['207,210(todo),444', '1188,130,855(again)', '26,27', '802,1197,26', '163,1048']
31+
2024-11-15 -> ['53,210', '26,27', '802,1197,26', '353', '947']
32+
2024-11-14 -> ['207,444', '951,792', '528,334']
33+
2024-11-13 -> ['53,210', '1188,130,855(again)']
34+
2024-11-12 -> ['53,210', '207,444', '1145', '753']
35+
2024-11-11 -> ['53,210', '207,444', '1188,130,855(again)', '900', '727']
36+
2024-11-10 -> ['207,444', '1188,130,855(again)', '26,27', '802,1197,26', '163,1048']
3537
2024-11-09 -> ['1188,130,855(again)', '855,846', '298,729', '1145,1219']
3638
2024-11-08 -> ['900', '932', '1146']
3739
2024-11-07 -> ['26,27', '802,1197,26']
@@ -300,7 +302,7 @@
300302
2023-12-10 -> ['23', '134']
301303
2023-12-09 -> ['19,003', '55,45']
302304
2023-12-08 -> ['19,003', '23']
303-
2023-12-07 -> ['53', '19,003', '23', '55,45']
305+
2023-12-07 -> ['19,003', '23', '55,45', '53']
304306
2023-12-06 -> ['23', '55,45']
305307
2023-12-05 -> ['55,45']
306308
2023-11-30 -> ['quick_sort,286']
@@ -310,13 +312,13 @@
310312
2023-11-24 -> ['53', '133,695']
311313
2023-11-21 -> ['452,406,135']
312314
2023-11-19 -> ['134']
313-
2023-11-16 -> ['53', '452,406,135']
315+
2023-11-16 -> ['452,406,135', '53']
314316
2023-11-14 -> ['134']
315317
2023-11-13 -> ['452,406,135']
316-
2023-11-11 -> ['53', '452,406,135', '134']
318+
2023-11-11 -> ['452,406,135', '134', '53']
317319
2023-11-10 -> ['452,406,135']
318320
2023-11-09 -> ['452,406,135', '134', 'quick_sort,286']
319-
2023-11-08 -> ['53', '134']
321+
2023-11-08 -> ['134', '53']
320322
2023-11-07 -> ['134']
321323
2023-11-06 -> ['53', '994']
322324
2023-11-05 -> ['53']
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package LeetCodeJava.BFS;
2+
3+
// https://leetcode.com/problems/course-schedule-ii/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
* 210. Course Schedule II
9+
* Solved
10+
* Medium
11+
* Topics
12+
* Companies
13+
* Hint
14+
* There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. 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.
15+
*
16+
* For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
17+
* Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: numCourses = 2, prerequisites = [[1,0]]
24+
* Output: [0,1]
25+
* Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
26+
* Example 2:
27+
*
28+
* Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
29+
* Output: [0,2,1,3]
30+
* Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
31+
* So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
32+
* Example 3:
33+
*
34+
* Input: numCourses = 1, prerequisites = []
35+
* Output: [0]
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= numCourses <= 2000
41+
* 0 <= prerequisites.length <= numCourses * (numCourses - 1)
42+
* prerequisites[i].length == 2
43+
* 0 <= ai, bi < numCourses
44+
* ai != bi
45+
* All the pairs [ai, bi] are distinct.
46+
* Seen this question in a real interview before?
47+
* 1/5
48+
* Yes
49+
* No
50+
* Accepted
51+
* 1.2M
52+
* Submissions
53+
*
54+
*/
55+
public class CourseSchedule2 {
56+
57+
// V0
58+
// IDEA : TOPOLOGICAL SORT (fixed by gpt)
59+
// ref : https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java
60+
public int[] findOrder(int numCourses, int[][] prerequisites) {
61+
if (numCourses == 1) {
62+
return new int[]{0};
63+
}
64+
65+
// topologic ordering
66+
67+
//return topoOrdering(numCourses, prerequisites);
68+
List<Integer> ordering = topologicalSort(numCourses, prerequisites);
69+
//System.out.println(">>> ordering = " + ordering);
70+
if (ordering == null){
71+
return new int[]{};
72+
}
73+
int[] res = new int[numCourses];
74+
for (int x = 0; x < ordering.size(); x++) {
75+
int val = ordering.get(x);
76+
//System.out.println(val);
77+
res[x] = val;
78+
}
79+
80+
return res;
81+
}
82+
83+
public List<Integer> topologicalSort(int numNodes, int[][] edges) {
84+
// Step 1: Build the graph and calculate in-degrees
85+
Map<Integer, List<Integer>> graph = new HashMap<>();
86+
int[] inDegree = new int[numNodes];
87+
88+
for (int i = 0; i < numNodes; i++) {
89+
graph.put(i, new ArrayList<>());
90+
}
91+
92+
for (int[] edge : edges) {
93+
int from = edge[0];
94+
int to = edge[1];
95+
graph.get(from).add(to);
96+
inDegree[to]++;
97+
}
98+
99+
// Step 2: Initialize a queue with nodes that have in-degree 0
100+
Queue<Integer> queue = new LinkedList<>();
101+
for (int i = 0; i < numNodes; i++) {
102+
/**
103+
* NOTE !!!
104+
*
105+
* we add ALL nodes with degree = 0 to queue at init step
106+
*/
107+
if (inDegree[i] == 0) {
108+
queue.offer(i);
109+
}
110+
}
111+
112+
List<Integer> topologicalOrder = new ArrayList<>();
113+
114+
// Step 3: Process the nodes in topological order
115+
while (!queue.isEmpty()) {
116+
/**
117+
* NOTE !!!
118+
*
119+
* ONLY "degree = 0" nodes CAN be added to queue
120+
*
121+
* -> so we can add whatever node from queue to final result (topologicalOrder)
122+
*/
123+
int current = queue.poll();
124+
topologicalOrder.add(current);
125+
126+
for (int neighbor : graph.get(current)) {
127+
inDegree[neighbor] -= 1;
128+
/**
129+
* NOTE !!!
130+
*
131+
* if a node "degree = 0" means this node can be ACCESSED now,
132+
*
133+
* -> so we need to add it to the queue (for adding to topologicalOrder in the following while loop iteration)
134+
*/
135+
if (inDegree[neighbor] == 0) {
136+
queue.offer(neighbor);
137+
}
138+
}
139+
}
140+
141+
// If topologicalOrder does not contain all nodes, there was a cycle in the graph
142+
if (topologicalOrder.size() != numNodes) {
143+
//throw new IllegalArgumentException("The graph has a cycle, so topological sort is not possible.");
144+
return null;
145+
}
146+
147+
/** NOTE !!! reverse ordering */
148+
Collections.reverse(topologicalOrder);
149+
return topologicalOrder;
150+
}
151+
152+
// V1
153+
154+
// V2
155+
}

0 commit comments

Comments
 (0)