Skip to content

Commit 1bf6cfe

Browse files
authored
Improved task 2368.
1 parent 4fed31d commit 1bf6cfe

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ implementation 'com.github.javadev:leetcode-in-java:1.12'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2370 |[Longest Ideal Subsequence](src/main/java/g2301_2400/s2370_longest_ideal_subsequence/Solution.java)| Medium | String, Hash_Table, Dynamic_Programming | 28 | 85.71
1852+
| 2369 |[Check if There is a Valid Partition For The Array](src/main/java/g2301_2400/s2369_check_if_there_is_a_valid_partition_for_the_array/Solution.java)| Medium | Array, Dynamic_Programming | 7 | 81.82
1853+
| 2368 |[Reachable Nodes With Restrictions](src/main/java/g2301_2400/s2368_reachable_nodes_with_restrictions/Solution.java)| Medium | Array, Tree, Graph, Hash_Table, Depth_First_Search, Breadth_First_Search | 59 | 85.71
18511854
| 2367 |[Number of Arithmetic Triplets](src/main/java/g2301_2400/s2367_number_of_arithmetic_triplets/Solution.java)| Easy | Array, Enumeration, Hash_Table, Two_Pointers | 3 | 66.67
18521855
| 2366 |[Minimum Replacements to Sort the Array](src/main/java/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.java)| Hard | Array, Math, Greedy | 10 | 28.57
18531856
| 2365 |[Task Scheduler II](src/main/java/g2301_2400/s2365_task_scheduler_ii/Solution.java)| Medium | Array, Simulation, Hash_Table | 70 | 55.56
Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
package g2301_2400.s2368_reachable_nodes_with_restrictions;
22

33
// #Medium #Array #Tree #Graph #Hash_Table #Depth_First_Search #Breadth_First_Search
4-
// #2022_08_15_Time_88_ms_(42.86%)_Space_201.1_MB_(14.29%)
4+
// #2022_08_16_Time_59_ms_(85.71%)_Space_89.6_MB_(85.71%)
55

6-
import java.util.LinkedList;
6+
import java.util.ArrayDeque;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Queue;
710

11+
@SuppressWarnings("unchecked")
812
public class Solution {
913
public int reachableNodes(int n, int[][] edges, int[] restricted) {
10-
LinkedList<Integer>[] adj = new LinkedList[n];
11-
for (int i = 0; i < n - 1; i++) {
12-
if (adj[edges[i][0]] == null) {
13-
LinkedList<Integer> source = new LinkedList<>();
14-
source.add(edges[i][1]);
15-
adj[edges[i][0]] = source;
16-
} else {
17-
adj[edges[i][0]].add(edges[i][1]);
18-
}
19-
if (adj[edges[i][1]] == null) {
20-
LinkedList<Integer> dest = new LinkedList<>();
21-
dest.add(edges[i][0]);
22-
adj[edges[i][1]] = dest;
23-
} else {
24-
adj[edges[i][1]].add(edges[i][0]);
25-
}
14+
List<Integer>[] graph = new ArrayList[n];
15+
for (int i = 0; i < n; i++) {
16+
graph[i] = new ArrayList<>();
2617
}
27-
boolean[] visited = new boolean[n];
28-
for (int res : restricted) {
29-
visited[res] = true;
18+
for (int[] edge : edges) {
19+
int src = edge[0];
20+
int dest = edge[1];
21+
graph[src].add(dest);
22+
graph[dest].add(src);
3023
}
31-
int count = 1;
24+
Queue<Integer> q = new ArrayDeque<>();
25+
boolean[] visited = new boolean[n];
26+
q.offer(0);
3227
visited[0] = true;
33-
return countReachableNodes(0, adj, visited, count);
34-
}
35-
36-
private int countReachableNodes(
37-
int source, LinkedList<Integer>[] adj, boolean[] visited, int count) {
38-
for (int neighbour : adj[source]) {
39-
if (!visited[neighbour]) {
40-
visited[neighbour] = true;
41-
count = countReachableNodes(neighbour, adj, visited, ++count);
28+
for (int node : restricted) {
29+
visited[node] = true;
30+
}
31+
int ans = 0;
32+
while (!q.isEmpty()) {
33+
int vertex = q.poll();
34+
ans++;
35+
for (int neighbour : graph[vertex]) {
36+
if (!visited[neighbour]) {
37+
q.offer(neighbour);
38+
visited[neighbour] = true;
39+
}
4240
}
4341
}
44-
return count;
42+
return ans;
4543
}
4644
}

0 commit comments

Comments
 (0)