Practice
n nodes and a list of edges. You want to find the smallest set of nodes such that starting a traversal from these nodes can reach every other node in the graph. Which approach guarantees finding this minimal set efficiently?Solution
Step 1: Understand the problem constraints
The problem requires the minimal set of vertices from which all nodes are reachable. Nodes with zero in-degree are exactly those that no other node reaches, so they must be included.Step 2: Analyze approaches
Performing BFS from each node is inefficient. Dynamic programming counting paths is unnecessary. The minimal set is precisely all nodes with zero in-degree.Final Answer:
Option C -> Option CQuick Check:
Minimal set corresponds exactly to zero in-degree nodes [OK]
- Assuming BFS from each node is efficient
- Confusing zero in-degree nodes with minimal set without justification
- Using DP which is unnecessary here
times = [[1,2,1],[2,3,2],[1,3,4]], n = 3, and k = 1, what is the final returned value?Solution
Step 1: Initialize distances with dist[1]=0, dist[2]=inf, dist[3]=inf
After first iteration, relax edges: dist[2]=1 (via 1->2), dist[3]=4 (via 1->3)Step 2: Second iteration relaxes edge 2->3: dist[3] updated to dist[2]+2=3 which is less than 4
No further updates after this iteration, so loop breaks earlyFinal Answer:
Option B -> Option BQuick Check:
Maximum distance is 3, so return 3 [OK]
- Not updating dist[3] to 3 after second iteration
- Returning initial max distance 4 instead of updated 3
- Confusing iteration count and stopping early
V courses and E prerequisite pairs?Solution
Step 1: Identify operations on nodes and edges
Each node is enqueued and dequeued once; each edge is visited once when decrementing in-degree.Step 2: Combine to get total complexity
Total time is proportional to number of nodes plus number of edges -> O(V + E).Final Answer:
Option A -> Option AQuick Check:
Standard BFS topological sort runs in linear time relative to graph size [OK]
- Assuming nested loops cause O(V*E)
- Confusing dense graph worst case with typical complexity
- Ignoring that BFS processes nodes once
Solution
Step 1: Identify graph size
The grid has n*m nodes and up to 4 edges per node, so edges E = O(n*m).Step 2: Analyze Dijkstra's complexity
Using a min-heap, each node is pushed/popped at most once, heap operations cost O(log(n*m)), total O(n*m*log(n*m)).Final Answer:
Option B -> Option BQuick Check:
Log factor comes from heap operations on n*m nodes [OK]
- Confusing edges with nodes count
- Assuming O(n*m^2) due to nested loops
- Ignoring log factor from heap
Solution
Step 1: Understand the new connectivity rule
Islands now connect horizontally, vertically, and diagonally, so DFS must explore all 8 neighbors.Step 2: Modify DFS to include diagonal neighbors
Add recursive DFS calls for (r+1,c+1), (r+1,c-1), (r-1,c+1), and (r-1,c-1) along with the original four directions.Final Answer:
Option A -> Option AQuick Check:
Only adding diagonal neighbors to DFS ensures correct island counting with 8-direction connectivity [OK]
- Assuming BFS handles diagonals without code change
- Counting diagonals separately after DFS
