1436. Destination City

EasyHash TableString
Leetcode Link

Problem Description

You're presented with an array called paths, where each element is itself an array of two cities, cityA and cityB. The sub-array [cityA, cityB] signifies that there is a direct path from cityA to cityB. Your task is to determine the destination city. The destination city is defined as the city that doesn't have any outgoing paths to other cities. Hence, it only appears as the second element in one of the sub-arrays and never as the first element. Given the graph of paths forms a straight line, meaning that it doesn’t loop back on itself, you can be certain there is only one destination city.


For the solution, a set called s is created to hold all the cities that have outgoing paths; these are the first elements (cityA) in each sub-array of paths. Since the graph is guaranteed to be linear without loops, the destination city will never appear as a cityA. With this knowledge, the solution iterates over the paths again, checking the second element of each sub-array (cityB) to see if it doesn't exist in the set s. If cityB isn't in s, that means it has no outgoing paths and is thus the destination city. This result is then returned. Using a set for s is efficient as it allows for constant time complexity checks for the presence of a city in the set.

Solution Approach

The implementation of the solution involves the use of a Python set data structure and list comprehension, both of which are efficient in their respective roles.

  1. Python Set: The line s = {a for a, _ in paths} initiates the set that stores all the departure cities (cityA). The use of a set is critical here. Sets in Python store unique elements and they provide O(1) average time complexity for checking the presence or absence of an item. Since we only care about whether a city has outgoing paths, the set allows us to efficiently track cities we've seen as departure points.

  2. List Comprehension: The line return next(b for _, b in paths if b not in s) uses a list comprehension combined with the next function to find the destination city. This works by iterating over each path, _ is used to ignore the first element (since we're not interested in the departure cities anymore) and b refers to the potential destination cities.

    • The condition if b not in s checks if the city b is not in the set of departure cities we created earlier. Since the destination city will not have an outgoing path, it will not be found in the s set.
    • We use the next iterator function to immediately return the first occurrence of such a city. This improves efficiency because it stops the iteration as soon as the condition is met, and we don't have to check the rest of the paths.

The combination of a set for quick look-ups and an iterator for early exit provides a neat and efficient approach to solving this problem.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

What's the output of running the following function using input [30, 20, 10, 100, 33, 12]?

1def fun(arr: List[int]) -> List[int]:
2    import heapq
3    heapq.heapify(arr)
4    res = []
5    for i in range(3):
6        res.append(heapq.heappop(arr))
7    return res
1public static int[] fun(int[] arr) {
2    int[] res = new int[3];
3    PriorityQueue<Integer> heap = new PriorityQueue<>();
4    for (int i = 0; i < arr.length; i++) {
5        heap.add(arr[i]);
6    }
7    for (int i = 0; i < 3; i++) {
8        res[i] = heap.poll();
9    }
10    return res;
1class HeapItem {
2    constructor(item, priority = item) {
3        this.item = item;
4        this.priority = priority;
5    }
8class MinHeap {
9    constructor() {
10        this.heap = [];
11    }
13    push(node) {
14        // insert the new node at the end of the heap array
15        this.heap.push(node);
16        // find the correct position for the new node
17        this.bubble_up();
18    }
20    bubble_up() {
21        let index = this.heap.length - 1;
23        while (index > 0) {
24            const element = this.heap[index];
25            const parentIndex = Math.floor((index - 1) / 2);
26            const parent = this.heap[parentIndex];
28            if (parent.priority <= element.priority) break;
29            // if the parent is bigger than the child then swap the parent and child
30            this.heap[index] = parent;
31            this.heap[parentIndex] = element;
32            index = parentIndex;
33        }
34    }
36    pop() {
37        const min = this.heap[0];
38        this.heap[0] = this.heap[this.size() - 1];
39        this.heap.pop();
40        this.bubble_down();
41        return min;
42    }
44    bubble_down() {
45        let index = 0;
46        let min = index;
47        const n = this.heap.length;
49        while (index < n) {
50            const left = 2 * index + 1;
51            const right = left + 1;
53            if (left < n && this.heap[left].priority < this.heap[min].priority) {
54                min = left;
55            }
56            if (right < n && this.heap[right].priority < this.heap[min].priority) {
57                min = right;
58            }
59            if (min === index) break;
60            [this.heap[min], this.heap[index]] = [this.heap[index], this.heap[min]];
61            index = min;
62        }
63    }
65    peek() {
66        return this.heap[0];
67    }
69    size() {
70        return this.heap.length;
71    }
74function fun(arr) {
75    const heap = new MinHeap();
76    for (const x of arr) {
77        heap.push(new HeapItem(x));
78    }
79    const res = [];
80    for (let i = 0; i < 3; i++) {
81        res.push(heap.pop().item);
82    }
83    return res;

Example Walkthrough

Let's take a small example to illustrate the solution approach: Imagine we have the following paths array:

1paths = [["London", "New York"], ["New York", "Paris"], ["Paris", "Berlin"]]

Here, each sub-array represents a direct flight path from one city to another. By examining the data, we need to find out which city is our final destination (a city with no outgoing flights).

  1. Creating a Set of Departure Cities:

    • First, we create a set s to keep track of all departure cities (cities with outgoing flights), which would be {"London", "New York", "Paris"} in our example. This set is created using a set comprehension: s = {a for a, _ in paths}.
  2. Finding the Destination City:

    • Next, we find the destination city using a list comprehension inside the next function: return next(b for _, b in paths if b not in s).
    • The list comprehension iterates over the paths:
      • For the first sub-array: ["London", "New York"], New York is checked against the set s. It's in the set, so we move on.
      • For the second sub-array: ["New York", "Paris"], Paris is checked against the set s. It's also in the set, so we move on.
      • For the third sub-array: ["Paris", "Berlin"], Berlin is checked against the set s. It's not in the set, so Berlin is our destination city because it clearly has no outgoing flights.

Since Berlin is not in the set of departure cities, it is immediately returned by the next function as the destination city. There is no need to check further. Thus, the answer to our example would be "Berlin".

This approach is highly efficient because it avoids checking the entire array for the destination city and makes a quick determination using the set.

Solution Implementation

1class Solution:
2    def destCity(self, paths: List[List[str]]) -> str:
3        # Create a set of departure cities
4        departures = {path[0] for path in paths}
6        # Iterate through each pair of cities in paths
7        for _, destination in paths:
8            # If the destination city is not in the set of departures
9            if destination not in departures:
10                # It must be the destination city we are looking for, return it
11                return destination
13# The `paths` parameter is expected to be a list where each element is another list
14# of two strings, representing a departure city and a destination city, respectively.
16# The `destCity` method will return the name of the city that is the destination city.
17# A destination city is defined as one that is not a departure city for any city-path
18# within the 'paths' list. In other words, it has no outgoing paths.
21In this rewritten code, I have:
231. Followed the Python style guide (PEP 8) for variable naming by renaming the variable `s` to `departures` to make its purpose clearer.
242. I have replaced the set comprehension with a more standard loop format that should be easier for many programmers to understand.
253. I've included comments that describe the logic of the code block and the purpose of variables and methods.
27Note that Python's 'typing' module needs to be imported to use type hints like `List`:
30from typing import List
1class Solution {
2    public String destCity(List<List<String>> paths) {
3        // Create a HashSet to store all departure cities
4        Set<String> departureCities = new HashSet<>();
6        // Iterate over the list of paths and add all starting cities to the HashSet
7        for (List<String> path : paths) {
8            departureCities.add(path.get(0));
9        }
11        // Iterate over the list of paths to find the destination city
12        // The destination city will not be found in the set of departure cities
13        for (List<String> path : paths) {
14            if (!departureCities.contains(path.get(1))) {
15                // We found the destination city, return it
16                return path.get(1);
17            }
18        }
20        // If no unique destination city is found, return an empty string
21        return "";
22    }
1#include <vector>
2#include <string>
3#include <unordered_set>
5class Solution {
7    // Function to find the destination city from a list of paths.
8    // Each path is represented as a vector of two strings, where the first string is the starting city and the second is the destination city.
9    string destCity(vector<vector<string>>& paths) {
10        // Create an unordered set to store the starting cities.
11        unordered_set<string> startingCities;
13        // Populate the startingCities set with starting cities from the paths.
14        for (auto& path : paths) {
15            startingCities.insert(path.front()); // The starting city is at the front of each path.
16        }
18        // Loop through the paths again and identify the destination city that is not in the startingCities set.
19        // This city is the final destination, since no paths originate from it.
20        for (auto& path : paths) {
21            // If the destination city in the current path is not found in the startingCities set,
22            // it means we have found the destination city.
23            if (startingCities.count(path.back()) == 0) { // The destination city is at the back of each path.
24                return path.back();
25            }
26        }
28        // If all cities appear as a starting city, return an empty string.
29        // This scenario shouldn't happen according to the problem's constraints but is used as a safe guard.
30        return "";
31    }
1// This function finds the destination city from a list of origin-destination path pairs.
2// paths: A list of pairs where each pair contains the origin and destination city names.
3function destCity(paths: string[][]): string {
4    // Create a Set to store just the origin cities for quick lookup.
5    const originCities = new Set<string>();
7    // Populate the Set with just the first element (origin) from each path.
8    paths.forEach(path => {
9        const origin = path[0];
10        originCities.add(origin);
11    });
13    // Iterate over the paths to find a city that is not an origin city.
14    for (const path of paths) {
15        const destination = path[1];
16        // If the current city is not in the set of origin cities, it's our destination.
17        if (!originCities.has(destination)) {
18            return destination;
19        }
20    }
22    // If all cities are found as origins, there's no destination city which should not happen.
23    // Return an empty string (this case is assumed to not occur as per the problem statement).
24    return '';

Time and Space Complexity

The given Python code defines a method destCity to find the destination city from a list of city paths. A path is represented by a list of two strings, where the first string is the starting city and the second is the destination city. We want to find the destination city that is not the starting point of any path.

Time Complexity

The time complexity of the code can be determined by looking at the operations performed:

  1. Creating a set s of starting cities: This operation traverses the list of paths once. The time complexity of this operation is O(n), where n is the number of paths since set insertions have an average time complexity of O(1) per insertion.

  2. Finding the destination city which is not in the starting cities set s: This involves another traversal through the paths list. For each destination city, we check whether it is not in s. The complexity for the search operation for sets is O(1) on average. Therefore, this step also has a time complexity of O(n).

Since these operations are sequential, the overall time complexity of the code is O(n).

Space Complexity

The space complexity is based on the additional memory used by the code:

  1. A set s created to store starting cities. In the worst-case scenario, all cities are unique, and hence, s would contain n elements. This gives us a space complexity of O(n).

There are no other significant data structures utilized. Thus, the total space complexity of the method is O(n).

In summary:

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Learn more about how to find time and space complexity quickly using problem constraints.

Fast Track Your Learning with Our Quick Skills Quiz:

Which algorithm should you use to find a node that is close to the root of the tree?

Recommended Readings

Got a question? Ask the Monster Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.