LeetCode 2368: Reachable Nodes With Restrictions Solution
Master LeetCode problem 2368 (Reachable Nodes With Restrictions), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2368. Reachable Nodes With Restrictions
Problem Explanation
Explanation:
To solve this problem, we can perform a Depth First Search (DFS) traversal on the tree starting from node 0 while keeping track of the restricted nodes. We can use a priority queue to prioritize visiting nodes that are not restricted. We will also maintain a set to keep track of visited nodes.
- Initialize a graph data structure to store the edges.
- Perform a DFS traversal starting from node 0 and keep track of the number of nodes visited that are not restricted.
- Use a priority queue to prioritize visiting nodes that are not restricted.
- Maintain a set to keep track of visited nodes and restricted nodes.
- Keep track of the maximum number of nodes visited without visiting a restricted node.
- Return the maximum number of nodes reached from node 0 without visiting a restricted node.
Time Complexity: O(n log n) where n is the number of nodes. Space Complexity: O(n) for storing the graph and sets. :
Solution Code
import java.util.*;
class Solution {
public int reachableNodes(int n, int[][] edges, int[] restricted) {
Map<Integer, List<int[]>> graph = new HashMap<>();
Set<Integer> visited = new HashSet<>();
Set<Integer> restrictedSet = new HashSet<>();
for (int r : restricted) {
restrictedSet.add(r);
}
for (int[] edge : edges) {
int u = edge[0], v = edge[1];
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new int[]{v, 1});
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new int[]{u, 1});
}
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]);
pq.offer(new int[]{0, n});
int res = 0;
while (!pq.isEmpty()) {
int[] curr = pq.poll();
int node = curr[0];
int moves = curr[1];
if (visited.contains(node)) {
continue;
}
visited.add(node);
if (!restrictedSet.contains(node)) {
res++;
}
if (graph.containsKey(node)) {
for (int[] next : graph.get(node)) {
int nextNode = next[0];
int cost = next[1];
if (!visited.contains(nextNode)) {
int remainingMoves = moves - cost - 1;
if (remainingMoves >= 0) {
pq.offer(new int[]{nextNode, remainingMoves});
}
}
}
}
}
return res;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2368 (Reachable Nodes With Restrictions)?
This page provides optimized solutions for LeetCode problem 2368 (Reachable Nodes With Restrictions) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2368 (Reachable Nodes With Restrictions)?
The time complexity for LeetCode 2368 (Reachable Nodes With Restrictions) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2368 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2368 in Java, C++, or Python.