Destination City (LeetCode 1436): Solution and Explanation

Easy
Array
Hash Table
String

Each road goes from one city to another and the roads form a single line. Return the one city that no road leaves.

Intuition

Picture the trip drawn on a map. Every city along the way has exactly one road leaving it, except the last one, where the trip stops.

So the destination is the only city that never shows up as the start of a road. You do not need to rebuild the trip in order to find it.

Put every start city into a set. Then any end city that is missing from the set is the answer, and the constraints promise there is exactly one.

Approach: Set of cities with an exit

  1. Create an empty set has_exit.
  2. For every road [a, b], add a to has_exit.
  3. For every road [a, b], if b is not in has_exit, return b.
  4. The input always has a destination, so the final return "" is never reached.

Step-by-step example

paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
  1. Pass 1 reads the start cities: has_exit = {London, New York, Lima}.
  2. Pass 2, road 0 ends at New York. New York is in the set, so a road leaves it.
  3. Road 1 ends at Lima, which is also in the set.
  4. Road 2 ends at Sao Paulo, which is not in the set. Return "Sao Paulo".

Code: Python, Java, C++

1class Solution:
2    def destCity(self, paths: List[List[str]]) -> str:
3        has_exit = set()
4        for a, b in paths:
5            has_exit.add(a)
6        for a, b in paths:
7            if b not in has_exit:
8                return b
9        return ""
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Two passes over the n roads, each with an average O(1) set insert or lookup (times the name length, which is at most 10).

Space

O(n)

The set holds up to n start cities.

Other approaches

Follow the roads (time O(n), space O(n))

Build a map from start city to end city, begin at any city and keep following the map until a city has no entry. Same cost, slightly more code.

Nested scan (time O(n^2), space O(1))

For each end city, scan all roads to see whether any road starts there. No extra memory, fine for 100 roads.

Common mistakes

  • Collecting end cities instead of start cities: the start of the trip is then the one that looks special, which is the wrong answer.
  • Checking start cities in the second pass. The destination never appears as a start city, so it can only be found among the end cities.
  • Comparing names case-insensitively. "Lima" and "lima" are different cities.

Frequently asked questions

Why do I need two passes?

The set must hold every start city before any lookup can be trusted. A road later in the input might start at the city you are checking.

Can there be more than one answer?

No. The roads form one line with no loop, so exactly one city has no outgoing road.

Does the order of paths matter?

No. The roads can be listed in any order; the set does not care about the trip order.