Skip to content

Commit 517b427

Browse files
committed
2368. Reachable Nodes With Restrictions
1 parent 917284a commit 517b427

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ The project is divided into two parts: `structure` and `solution`.
277277
| 277 | [Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/) | Medium | [FindTheCelebrity.java](src/leetcode/solution/Graph/FindTheCelebrity.java) | - Logical Deduction |
278278
| 310 | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | Medium | [MinimumHeightTrees.java](src/leetcode/solution/Graph/MinimumHeightTrees.java) | - BFS: remove leaves |
279279
| 1192 | [Critical Connections in a Network](https://leetcode.com/problems/critical-connections-in-a-network/) | Hard | [CriticalConnectionsInANetwork.java](src/leetcode/solution/Graph/CriticalConnectionsInANetwork.java) | - DFS: find cycle and remove the edge. |
280+
| 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | Medium | [ReachableNodesWithRestrictions.java](src/leetcode/solution/Graph/ReachableNodesWithRestrictions.java) | - DFS. |
280281

281282

282283

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package leetcode.solution.Graph;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 2368. Reachable Nodes With Restrictions
7+
*/
8+
public class ReachableNodesWithRestrictions {
9+
10+
public static void main(String[] args) {
11+
ReachableNodesWithRestrictionsSolution solution = new ReachableNodesWithRestrictionsSolution();
12+
int n = 7;
13+
int[][] edges = new int[][]{{0, 1}, {0, 2}, {0, 5}, {0, 4}, {3, 2}, {6, 5}};
14+
int[] restricted = new int[]{4, 2, 1};
15+
16+
System.out.println(solution.reachableNodes(n, edges, restricted));
17+
// 3
18+
}
19+
20+
}
21+
22+
23+
class ReachableNodesWithRestrictionsSolution {
24+
25+
private List<List<Integer>> graph;
26+
private Set<Integer> set;
27+
private int count;
28+
29+
public int reachableNodes(int n, int[][] edges, int[] restricted) {
30+
this.graph = build(n, edges);
31+
this.set = new HashSet<>();
32+
33+
for (int num : restricted) {
34+
set.add(num);
35+
}
36+
37+
this.count = 0;
38+
39+
dfs(0);
40+
41+
return count;
42+
}
43+
44+
private void dfs(int node) {
45+
if (set.contains(node)) {
46+
return;
47+
}
48+
49+
count++;
50+
set.add(node);
51+
52+
List<Integer> nei = graph.get(node);
53+
54+
for (int next : nei) {
55+
dfs(next);
56+
}
57+
58+
}
59+
60+
private List<List<Integer>> build(int n, int[][] edges) {
61+
List<List<Integer>> list = new ArrayList<>();
62+
63+
for (int i = 0; i < n; i++) {
64+
list.add(new ArrayList<>());
65+
}
66+
67+
for (int[] edge : edges) {
68+
list.get(edge[0]).add(edge[1]);
69+
list.get(edge[1]).add(edge[0]);
70+
}
71+
72+
return list;
73+
}
74+
}

0 commit comments

Comments
 (0)