1436. Destination City - Explanation
Description
Each pair in paths describes a directed trip from its first city to its second city. Together, the paths form one continuous route without a cycle, although the pairs can appear in any order. Return the final city on the route: it has no outgoing trip. City names are case-sensitive and may contain spaces.
Example 1:
Input: paths = [["Cedar","Oak"],["Maple","Cedar"]]
Output: "Oak"Explanation: The route is Maple to Cedar to Oak. Oak has no outgoing trip, so it is the destination.
Example 2:
Input: paths = [["North Bay","South Bay"]]
Output: "South Bay"Explanation: The only trip ends at South Bay. There are no further trips from that city.
Example 3:
Input: paths = [["a","A"],["A","Z"]]
Output: "Z"Explanation: City names are case-sensitive: a and A are different cities. Following both trips ends at Z.
Constraints:
1 <= paths.length <= 100- Every path contains exactly two different city names.
- Each city name contains 1 to 10 English letters or spaces.
- The paths form one continuous directed route without cycles.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Hash Set - Used for O(1) lookup to check if a city is a starting point
- Hash Map - Used to model the path chain from starting city to destination city
1. Brute Force
Intuition
The destination city is a city that appears as an endpoint but never as a starting point in any path. For each destination in the paths, we can check whether it ever appears as a starting city. The city that never starts a path is our answer.
Algorithm
- Iterate through each path and consider its destination city.
- For each destination, iterate through all paths again to check if this city appears as a starting point.
- If the destination never appears as a starting point in any path, return it as the answer.
- Return an empty string if no destination city is found (though the problem guarantees one exists).
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Hash Set
Intuition
Instead of checking each destination against all starting points in O(n) time, we can store all starting cities in a hash set for O(1) lookup. Any destination city that is not in the set of starting cities must be the final destination.
Algorithm
- Create a hash set and add all starting cities (the first element of each path) to it.
- Iterate through all paths and check each destination city (the second element).
- If a destination city is not found in the hash set, return it as the answer.
- The first destination not in the set is the final destination city.
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Hash Map
Intuition
We can model the paths as a linked chain where each city points to its next destination. By building this chain in a hash map and following the links from any starting city, we will eventually reach the final destination, which has no outgoing path.
Algorithm
- Build a hash map where each key is a starting city and its value is the destination city.
- Start with the first city from the first path.
- Keep following the chain: while the current city exists as a key in the map, move to its destination.
- When the current city is not a key in the map, it means there is no outgoing path from it, return it as the destination city.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Adding Both Cities to the Set
When using the hash set approach, only starting cities should be added to the set. Adding both starting and ending cities defeats the purpose since you need to find which destination is never a starting point.
# Wrong - adds both cities
for p in paths:
s.add(p[0])
s.add(p[1]) # Incorrect: this includes destinations
# Correct - only add starting cities
for p in paths:
s.add(p[0])Checking Starting Cities Instead of Destinations
A common logic error is checking if starting cities are absent from the set of destinations, rather than checking if destinations are absent from the set of starting cities.
# Wrong - checking the wrong direction
s = set(p[1] for p in paths) # Set of destinations
for p in paths:
if p[0] not in s: # Looking for start with no incoming path
return p[0]
# Correct - find destination with no outgoing path
s = set(p[0] for p in paths) # Set of starting cities
for p in paths:
if p[1] not in s: # Destination that is never a start
return p[1]
Sign in to join the discussion