One-way flight trip problem

后端 未结 18 1515

You are going on a one-way indirect flight trip that includes billions an unknown very large number of transfers.

  • You are not stoppi
18条回答
  •  被撕碎了的回忆
    2020-12-12 17:43

    Put in two Hashes: to_end = src -> des; to_beg = des -> src

    Pick any airport as a starting point S.

    while(to_end[S] != null)
       S = to_end[S];
    

    S is now your final destination. Repeat with the other map to find your starting point.

    Without properly checking, this feels O(N), provided you have a decent Hash table implementation.

提交回复
热议问题