Destination City - Complete Solution Guide
Destination City is LeetCode problem 1436, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given the array paths , where paths[i] = [cityA i , cityB i ] means there exists a direct path going from cityA i to cityB i . Return the destination city, that is, the city without any path outgoing to another city. It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city. Example 1: Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]] Output: "Sao Paulo" Explanation: Starting at "London" cit
Detailed Explanation
The problem asks you to find the destination city in a list of paths. Each path is represented as a list of two strings: the starting city and the destination city. The destination city is the one that is never a starting city in any path. The input is a list of these paths, and the output is a single string representing the destination city. It's guaranteed that there will be exactly one destination city and that the paths form a directed acyclic graph (DAG) – meaning there are no cycles.
Solution Approach
The provided solutions utilize a set to store the starting cities. They iterate through the paths, adding each starting city to the set. Then, they iterate again, checking if each ending city is present in the set of starting cities. If an ending city is not found in the set, it is the destination city and is returned. This approach leverages the efficiency of set lookups.
Step-by-Step Algorithm
- Step 1: Initialize a set (HashSet in Java, unordered_set in C++, set in Python) to store the starting cities.
- Step 2: Iterate through the input list of paths. For each path, add the starting city (the first element in the path) to the set of starting cities.
- Step 3: Iterate through the input list of paths again. For each path, check if the ending city (the second element in the path) is present in the set of starting cities.
- Step 4: If the ending city is NOT in the set of starting cities, it means that there is no path leaving from this city and it's the destination city. Return this city.
- Step 5: If the loop completes without finding such a city (meaning the set was empty or there's an issue with the input), return an appropriate value (empty string or null).
Key Insights
- Insight 1: Using a set to efficiently track starting cities avoids redundant searches. Sets offer O(1) average time complexity for insertion and lookup.
- Insight 2: The destination city is the only city present in the list of end cities (cities that are destinations in the paths) but not in the list of starting cities.
- Insight 3: Handling the edge case of an empty input list of paths needs to be considered. The code should return an appropriate value (an empty string or null) in such situations.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String.
Companies
Asked at: Yandex, Yelp.