Reconstruct Island Visit Order
Problem statement
You visited a sequence of unique islands but forgot their exact order. Each pair in consecutiveVisits contains two islands that were visited consecutively; either island may have come first.
Reconstruct the complete visit order. Every island appears exactly once, and every adjacent pair in the returned order must correspond to one supplied pair.
The pairs are guaranteed to describe one simple path containing every island. A path and its reversal both satisfy the adjacency evidence, so return the orientation that starts from the numerically larger endpoint.
Function
reconstructIslandVisitOrder(consecutiveVisits: int[][]) → int[]Examples
Example 1
consecutiveVisits = [[1,2],[2,3],[4,5],[1,5]]return = [4,5,1,2,3]The pairs connect the path 4 - 5 - 1 - 2 - 3. Its endpoints are 3 and 4; the deterministic exercise convention starts from the larger endpoint, 4.
Constraints
1 <= consecutiveVisits.length <= 105- Every element of
consecutiveVisitscontains exactly two distinct island IDs in the range[-109, 109]. - The undirected pairs form one simple path and contain no duplicate edge.
- Return the path starting from the numerically larger of its two endpoints.