Back to photostream

Destination City Leetcode Solution

The problem Destination City Leetcode Solution provides us with some relations between cities. The input is given as line separated pair of cities. Each line in input denotes a direct road from the starting point to the endpoint. It is given in the problem, that the cities do not form a circular route. It is also stated that the input has a destination city. A destination city is defined as a city that does not have any outgoing road. So as usual before diving deep into the solution, let's take a look at a few examples.

 

paths = ,,]

Sao Paulo

Explanation: So if we go by the input, London has a direct road to New York. New York has a direct road to Lima. In the end, Lima has a direct road to Sao Paulo. So, the destination city must be Sao Paulo because it has no outgoing roads.

paths = ]

Z

Explanation: We have a single direct road starting from A to Z. Thus Z is our destination city.

Approach to Destination City Leetcode Solution

The problem Destination City Leetcode Solution asked us to find the destination. The input has provided us with some direct roads among the cities. It is given that a destination city does not have an outgoing road. The problem can be easily solved using a hashmap. In the hashmap, we keep track of outgoing roads. We traverse the paths vector and increment the number of outgoing roads of the cities. Then afterward we check if there is any city among the paths vector, that does not have an outgoing road. We return that city as the answer.

Code

 

www.tutorialcup.com/leetcode-solutions/destination-city-l...

53 views
0 faves
0 comments
Uploaded on August 31, 2021