Problem · Array

Reconstruct Landmark Journey

Learn this problem
MediumTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

A traveler visited a series of unique landmarks but no longer remembers the exact order. Each pair in travelPhotos shows two landmarks that were visited consecutively; either landmark may have been visited first.

Reconstruct the complete journey. Every landmark was visited exactly once, and every adjacent pair in the result must correspond to one supplied photo.

The photos are guaranteed to describe one simple path containing every landmark.

Function

solution(travelPhotos: int[][]) → int[]

Examples

Example 1

travelPhotos = [[3,5],[1,4],[2,4],[1,5]]return = [3,5,1,4,2]

The pairs connect the path 3 - 5 - 1 - 4 - 2. The endpoints are 3 and 2; 3 is encountered first in the input, so the deterministic result starts there.

Constraints

  • 1 <= travelPhotos.length
  • Every element of travelPhotos contains exactly two distinct landmark IDs.
  • The undirected pairs form one simple path and contain no duplicate edge.

More Tiktok problems

drafts saved locally
public int[] solution(int[][] travelPhotos) {
    // Write your code here.
}
travelPhotos[[3,5],[1,4],[2,4],[1,5]]
expected[3,5,1,4,2]
checking account