
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find airports in correct order in Python?
Suppose we have a list of flights as [origin, destination] pairs. The list is shuffled; we have to find all the airports that were visited in the correct order. If there are more than one valid itinerary, return lexicographically smallest ones first.
So, if the input is like flights = [["Mumbai", "Kolkata"],["Delhi", "Mumbai"],["Kolkata", "Delhi"] ], then the output will be ['Delhi', 'Mumbai', 'Kolkata', 'Delhi']
To solve this, we will follow these steps
ins := an empty map
outs := an empty map
adj_list := an empty map
Define a function dfs() . This will take airport
while outs[airport] is not null, do
nxt := size of adj_list[airport] - outs[airport]
outs[airport] := outs[airport] - 1
insert airport at the end of ans
Define a method called solve(), this will take flights
for each start end pair s, e in flights, do
insert e at the end of adj_list[s]
outs[s] := outs[s] + 1
ins[e] := ins[e] + 1
for each l in list of all values of adj_list, do
sort the list l
start := null, end := null
for each airport in list of all keys of adj_list, do
if outs[airport] - ins[airport] is same as 1, then
if start not null, then
return
start := airport
otherwise when outs[airport] - ins[airport] is same as -1, then
if end is not null, then
return
end := airport
otherwise when outs[airport] - ins[airport] is not same as 0, then
return
start := start if start is not null otherwise minimum of all keys of adj_list
ans := a new list
dfs(start)
return reverse of ans
From the main method call solve(flights)
Example
from collections import defaultdict class Solution: def solve(self, flights): ins = defaultdict(int) outs = defaultdict(int) adj_list = defaultdict(list) for s, e in flights: adj_list[s].append(e) outs[s] += 1 ins[e] += 1 for l in adj_list.values(): l.sort() start = None end = None for airport in adj_list.keys(): if outs[airport] - ins[airport] == 1: if start: return start = airport elif outs[airport] - ins[airport] == -1: if end: return end = airport elif outs[airport] - ins[airport] != 0: return start = start if start else min(adj_list.keys()) ans = [] def dfs(airport): while outs[airport]: nxt = len(adj_list[airport]) - outs[airport] outs[airport] -= 1 dfs(adj_list[airport][nxt]) ans.append(airport) dfs(start) return ans[::-1] ob = Solution() flights = [ ["Mumbai", "Kolkata"], ["Delhi", "Mumbai"], ["Kolkata", "Delhi"] ] print(ob.solve(flights))
Input
[["Mumbai", "Kolkata"], ["Delhi", "Mumbai"], ["Kolkata", "Delhi"] ]
Output
['Delhi', 'Mumbai', 'Kolkata', 'Delhi']
- Related Articles
- Program to find correct order of visited cities in C++
- Program to find squared elements list in sorted order in Python
- Program to find out the inheritance order in a family in Python
- Program to find longest substring of all vowels in order in Python
- Program to find overlapping intervals and return them in ascending order in Python
- Program to find dropped correct sensor value from the faulty list in Python
- Python program to sort string in custom order
- Program to find all prime factors of a given number in sorted order in Python
- Program to print matrix elements in spiral order in python
- Program to find the minimum cost to arrange the numbers in ascending or descending order in Python
- Program to find the final ranking of teams in order from highest to lowest rank in python
- Program to find k sublists with largest sums and return sums in ascending order in Python
- Program to find out the sum of numbers where the correct permutation can occur in python
- Program to merge intervals and sort them in ascending order in Python
- C++ program to check we can place dominos on colored cells in correct order or not
