Skip to content

Commit 6c891a1

Browse files
committed
add 802 java, progress
1 parent a86cd87 commit 6c891a1

File tree

6 files changed

+265
-8
lines changed

6 files changed

+265
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@
979979
756| [Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/) | [Python](./leetcode_python/Depth-First-Search/pyramid-transition-matrix.py) | _O(a^b)_ | _O(a^b)_ | Medium || AGAIN (3) (not start)
980980
785| [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [Python](./leetcode_python/Depth-First-Search/is-graph-bipartite.py) | _O(\|V\| + \|E\|)_ | _O(\|V\|)_ | Medium |`check #886 Possible Bipartition`,`AGAIN`,`graph`,`dfs`,`fb`, `amazon`| AGAIN*********** (6)
981981
797| [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Python](./leetcode_python/Depth-First-Search/all-paths-from-source-to-target.py) | _O(p + r * n)_ | _O(n)_ | Medium |`graph`, `dfs classics`, `good basic`| AGAIN** (3)
982-
802| [Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/) | [Python](./leetcode_python/Depth-First-Search/find-eventual-safe-states.py) | _O(\|V\| + \|E\|)_ | _O(\|V\|)_ | Medium |`Topological Sort`| AGAIN (3) (not start)
982+
802| [Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/) | [Python](./leetcode_python/Depth-First-Search/find-eventual-safe-states.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/FindEventualSafeStates.java) | _O(\|V\| + \|E\|)_ | _O(\|V\|)_ | Medium |`Topological Sort`, dfs, bfd, graph, google| AGAIN (4) (not start)
983983
841| [Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/) | [Python](./leetcode_python/Depth-First-Search/keys-and-rooms.py) | _O(n!)_ | _O(n)_ | Medium |`google`, `bfs`, `dfs`, `good basic`| AGAIN*** (3)
984984
851| [Loud and Rich](https://leetcode.com/problems/loud-and-rich/) | [Python](./leetcode_python/Depth-First-Search/loud-and-rich.py) | _O(q + r)_ | _O(q + r)_ | Medium |`amazon`,`defaultdict`, `good basic`| AGAIN*** (3)
985985
1087| [Brace Explansion](https://leetcode.com/problems/brace-expansion/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/BraceExpansion.java) | _O(q + r)_ | _O(q + r)_ | Medium |dfs, backtrack, good trick, google| AGAIN*** (1)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-02
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-27
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241102: 802
12
20241027: 855,846
23
20241026: 932
34
20241024: 951,792

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-27 -> ['802']
12
2024-12-21 -> ['855,846']
23
2024-12-20 -> ['932']
34
2024-12-18 -> ['951,792']
@@ -6,32 +7,33 @@
67
2024-12-12 -> ['1146']
78
2024-12-08 -> ['737']
89
2024-12-07 -> ['686,734,737']
9-
2024-12-06 -> ['353']
10+
2024-12-06 -> ['802', '353']
1011
2024-12-05 -> ['528,334']
1112
2024-12-03 -> ['1145']
1213
2024-11-30 -> ['855,846', '1145,1219']
1314
2024-11-29 -> ['932']
1415
2024-11-27 -> ['951,792', '524,221,889']
1516
2024-11-26 -> ['743,889']
1617
2024-11-25 -> ['837']
17-
2024-11-23 -> ['163,1048', '981']
18+
2024-11-23 -> ['802', '163,1048', '981']
1819
2024-11-22 -> ['298,729', '1087']
1920
2024-11-21 -> ['1146']
2021
2024-11-20 -> ['939']
2122
2024-11-18 -> ['430']
2223
2024-11-17 -> ['855,846', '737', '363']
2324
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
24-
2024-11-15 -> ['353', '947']
25+
2024-11-15 -> ['802', '353', '947']
2526
2024-11-14 -> ['951,792', '528,334']
2627
2024-11-12 -> ['1145', '753']
2728
2024-11-11 -> ['727']
28-
2024-11-10 -> ['163,1048']
29+
2024-11-10 -> ['802', '163,1048']
2930
2024-11-09 -> ['855,846', '298,729', '1145,1219']
3031
2024-11-08 -> ['932', '1146']
32+
2024-11-07 -> ['802']
3133
2024-11-06 -> ['951,792', '524,221,889']
32-
2024-11-05 -> ['743,889']
33-
2024-11-04 -> ['855,846', '737', '837', '659']
34-
2024-11-03 -> ['932', '686,734,737', '801,552']
34+
2024-11-05 -> ['802', '743,889']
35+
2024-11-04 -> ['802', '855,846', '737', '837', '659']
36+
2024-11-03 -> ['802', '932', '686,734,737', '801,552']
3537
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
3638
2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087']
3739
2024-10-31 -> ['932', '1146']
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package LeetCodeJava.DFS;
2+
3+
// https://leetcode.com/problems/find-eventual-safe-states/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
/**
11+
* 802. Find Eventual Safe States
12+
* Medium
13+
* Topics
14+
* Companies
15+
* There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].
16+
*
17+
* A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
18+
*
19+
* Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
20+
*
21+
*
22+
*
23+
* Example 1:
24+
*
25+
* Illustration of graph
26+
* Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
27+
* Output: [2,4,5,6]
28+
* Explanation: The given graph is shown above.
29+
* Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
30+
* Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
31+
* Example 2:
32+
*
33+
* Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
34+
* Output: [4]
35+
* Explanation:
36+
* Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
37+
*
38+
*
39+
* Constraints:
40+
*
41+
* n == graph.length
42+
* 1 <= n <= 104
43+
* 0 <= graph[i].length <= n
44+
* 0 <= graph[i][j] <= n - 1
45+
* graph[i] is sorted in a strictly increasing order.
46+
* The graph may contain self-loops.
47+
* The number of edges in the graph will be in the range [1, 4 * 104].
48+
* Seen this question in a real interview before?
49+
* 1/5
50+
* Yes
51+
* No
52+
* Accepted
53+
* 292.9K
54+
* Submissions
55+
* 454.2K
56+
* Acceptance Rate
57+
* 64.5%
58+
*
59+
*/
60+
public class FindEventualSafeStates {
61+
62+
// V0
63+
// TODO : implement
64+
// public List<Integer> eventualSafeNodes(int[][] graph) {
65+
//
66+
// }
67+
68+
// V1-1
69+
// https://leetcode.com/problems/find-eventual-safe-states/editorial/
70+
// IDEA : DFS
71+
public boolean dfs(int node, int[][] adj, boolean[] visit, boolean[] inStack) {
72+
// If the node is already in the stack, we have a cycle.
73+
if (inStack[node]) {
74+
return true;
75+
}
76+
if (visit[node]) {
77+
return false;
78+
}
79+
// Mark the current node as visited and part of current recursion stack.
80+
visit[node] = true;
81+
inStack[node] = true;
82+
for (int neighbor : adj[node]) {
83+
if (dfs(neighbor, adj, visit, inStack)) {
84+
return true;
85+
}
86+
}
87+
// Remove the node from the stack.
88+
inStack[node] = false;
89+
return false;
90+
}
91+
92+
public List<Integer> eventualSafeNodes_1_1(int[][] graph) {
93+
int n = graph.length;
94+
boolean[] visit = new boolean[n];
95+
boolean[] inStack = new boolean[n];
96+
97+
for (int i = 0; i < n; i++) {
98+
dfs(i, graph, visit, inStack);
99+
}
100+
101+
List<Integer> safeNodes = new ArrayList<>();
102+
for (int i = 0; i < n; i++) {
103+
if (!inStack[i]) {
104+
safeNodes.add(i);
105+
}
106+
}
107+
108+
return safeNodes;
109+
}
110+
111+
// V1-2
112+
// https://leetcode.com/problems/find-eventual-safe-states/editorial/
113+
// IDEA : Topological Sort Using Kahn's Algorithm
114+
public List<Integer> eventualSafeNodes_1_2(int[][] graph) {
115+
int n = graph.length;
116+
int[] indegree = new int[n];
117+
List<List<Integer>> adj = new ArrayList<>();
118+
119+
for(int i = 0; i < n; i++) {
120+
adj.add(new ArrayList<>());
121+
}
122+
123+
for (int i = 0; i < n; i++) {
124+
for (int node : graph[i]) {
125+
adj.get(node).add(i);
126+
indegree[i]++;
127+
}
128+
}
129+
130+
Queue<Integer> q = new LinkedList<>();
131+
// Push all the nodes with indegree zero in the queue.
132+
for (int i = 0; i < n; i++) {
133+
if (indegree[i] == 0) {
134+
q.add(i);
135+
}
136+
}
137+
138+
boolean[] safe = new boolean[n];
139+
while (!q.isEmpty()) {
140+
int node = q.poll();
141+
safe[node] = true;
142+
143+
for (int neighbor : adj.get(node)) {
144+
// Delete the edge "node -> neighbor".
145+
indegree[neighbor]--;
146+
if (indegree[neighbor] == 0) {
147+
q.add(neighbor);
148+
}
149+
}
150+
}
151+
152+
List<Integer> safeNodes = new ArrayList<>();
153+
for (int i = 0; i < n; i++) {
154+
if (safe[i]) {
155+
safeNodes.add(i);
156+
}
157+
}
158+
return safeNodes;
159+
}
160+
161+
// V2
162+
// IDEA : DFS
163+
// https://leetcode.com/problems/find-eventual-safe-states/solutions/3752567/cycle-detection-same-code-begineers-friendly-c-java-python/
164+
public boolean dfs2(List<List<Integer>> adj, int src, boolean[] vis, boolean[] recst) {
165+
vis[src] = true;
166+
recst[src] = true;
167+
for (int x : adj.get(src)) {
168+
if (!vis[x] && dfs2(adj, x, vis, recst)) {
169+
return true;
170+
} else if (recst[x]) {
171+
return true;
172+
}
173+
}
174+
recst[src] = false;
175+
return false;
176+
}
177+
178+
public List<Integer> eventualSafeNodes_2(int[][] graph) {
179+
int n = graph.length;
180+
List<List<Integer>> adj = new ArrayList<>();
181+
for (int i = 0; i < n; i++) {
182+
List<Integer> list = new ArrayList<>();
183+
for (int j = 0; j < graph[i].length; j++) {
184+
list.add(graph[i][j]);
185+
}
186+
adj.add(list);
187+
}
188+
boolean[] vis = new boolean[n];
189+
boolean[] recst = new boolean[n];
190+
for (int i = 0; i < n; i++) {
191+
if (!vis[i]) {
192+
dfs2(adj, i, vis, recst);
193+
}
194+
}
195+
List<Integer> ans = new ArrayList<>();
196+
for (int i = 0; i < recst.length; i++) {
197+
if (!recst[i]) {
198+
ans.add(i);
199+
}
200+
}
201+
return ans;
202+
}
203+
204+
205+
// V3
206+
// IDEA : BFS
207+
// https://leetcode.com/problems/find-eventual-safe-states/solutions/3753320/c-java-python-bfs-dfs-topological-sort-easy-to-understand/
208+
public List<Integer> eventualSafeNodes_3(int[][] graph) {
209+
// Using BFS
210+
int n = graph.length;
211+
int[] indegree = new int[n];
212+
List<List<Integer>> g = new ArrayList<>(n);
213+
for (int i = 0; i < n; i++) {
214+
g.add(new ArrayList<>());
215+
}
216+
for (int i = 0; i < n; i++) {
217+
for (int itt : graph[i]) {
218+
g.get(itt).add(i); // reverse the direction of node
219+
indegree[i]++;
220+
}
221+
}
222+
Queue<Integer> q = new LinkedList<>();
223+
for (int i = 0; i < n; i++) {
224+
if (indegree[i] == 0) {
225+
q.add(i);
226+
}
227+
}
228+
List<Integer> ans = new ArrayList<>();
229+
while (!q.isEmpty()) {
230+
int node = q.poll();
231+
ans.add(node);
232+
for (int it : g.get(node)) {
233+
indegree[it]--;
234+
if (indegree[it] == 0)
235+
q.add(it);
236+
}
237+
}
238+
ans.sort(null);
239+
return ans;
240+
}
241+
242+
// V4
243+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,14 @@ public boolean isNStraightHand(int[] hand, int groupSize) {
22012201
return true;
22022202
}
22032203

2204+
// LC 802
2205+
// 5,28 - 5.40 PM
2206+
// https://leetcode.com/problems/find-eventual-safe-states/
2207+
public List<Integer> eventualSafeNodes(int[][] graph) {
2208+
2209+
return null;
2210+
}
2211+
22042212

22052213
}
22062214

0 commit comments

Comments
 (0)