Archipelago Explorer - Problem

An explorer is on island 0 in an archipelago of n islands numbered 0 to n - 1.

You are given a 2D array bridges where bridges[i] = [u, v] means a two-way bridge connects island u and island v.

Return the total number of islands the explorer can visit (including island 0).

Input & Output

Example 1
$ Input: n=7, bridges=[[0,1],[0,2],[1,3],[4,5],[5,6]]
Output: 4
💡 Note: Islands 0,1,2,3 reachable. 4,5,6 disconnected.
Example 2
$ Input: n=4, bridges=[[0,1],[1,2],[2,3]]
Output: 4
💡 Note: All reachable via chain.
Example 3
$ Input: n=5, bridges=[]
Output: 1
💡 Note: No bridges. Only island 0.

Constraints

  • 1 <= n <= 2*10^5
  • 0 <= bridges.length <= 2*10^5
  • 0 <= u, v < n

Visualization

Tap to expand
Archipelago Explorer INPUT Islands: n = 7, Start: island 0 Reachable 0 START 1 2 3 Unreachable 4 5 6 How many islands can the explorer visit from island 0? ALGORITHM STEPS 1 Build Bridge Map Adjacency list from bridges 2 BFS from Island 0 Queue: [0], Visited: {0} 3 Explore Bridges Visit all connected islands 4 Count Visited Return total visited count Execution Trace: step dequeue discover count init — 0 1 1 0 1,2 3 2 1 3 4 3 2 — 4 4 3 — 4 4 islands visited! FINAL RESULT Exploration Map: 🏝️ Visited Islands 0 1 2 3 🚫 Unreachable Islands 4, 5, 6 — no bridges Islands Reachable: 4 💡 Key Insight: The answer is the size of the connected component containing island 0. BFS from 0 counts reachable islands in O(V+E). Union-Find returns component size in near O(1). TutorialsPoint - Archipelago Explorer | BFS Exploration Approach
Asked in
Amazon 0
14 Views
High Frequency
~10 min Avg. Time
0 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen