
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Itinerary from a given list of tickets in C++
Suppose we have a list of tickets represented by pairs of departure and arrival airports like [from, to], we have to find the itinerary in order. All of the tickets belong to a man who departs from Chennai. So, the itinerary must begin with Chennai.
So if the input is like [["Mumbai", " Kolkata"], ["Chennai ", " Mumbai"], ["Delhi", "Bangalore"], ["Kolkata", " Delhi"]], then the output will be ["Chennai", " Mumbai", " Kolkata", " Delhi", "Bangalore"].
To solve this, we will follow these steps −
Define array ret and a map called graph.
Define a method called visit. This will take airport name as input
while size of the graph[airport] is not 0
x := first element of graph[airport]
delete the first element from graph[airport]
call visit(x)
insert airport into ret
Now from the main method, do the following −
for i in range 0 to size of tickers array
u := tickets[i, 0], v := tickets[i, 1], insert v into graph[u]
visit(“Chennai”) as this is the first airport
reverse the list ret and return
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <string> ret; map < string, multiset <string> > graph; vector<string> findItinerary(vector<vector<string>>& tickets) { for(int i = 0; i < tickets.size(); i++){ string u = tickets[i][0]; string v = tickets[i][1]; graph[u].insert(v); } visit("Chennai"); reverse(ret.begin(), ret.end()); return ret; } void visit(string airport){ while(graph[airport].size()){ string x = *(graph[airport].begin()); graph[airport].erase(graph[airport].begin()); visit(x); } ret.push_back(airport); } }; main(){ Solution ob; vector<vector<string>> v = {{"Mumbai", "Kolkata"}, {"Chennai", "Mumbai"}, {"Delhi", "Bangalore"}, {"Kolkata", "Delhi"}}; print_vector(ob.findItinerary(v)); }
Input
{{"Mumbai", "Kolkata"}, {"Chennai", "Mumbai"}, {"Delhi", "Bangalore"}, {"Kolkata", "Delhi"}}
Output
[Chennai, Mumbai, Kolkata, Delhi, Bangalore, ]
- Related Articles
- Reconstruct Itinerary in C++
- Program to find folded list from a given linked list in Python
- Create linked list from a given array in C++ Program
- Find number from given list for which value of the function is closest to A in C++
- Find A and B from list of divisors in C++
- Find the Kth Node from the end in a given singly Linked List using C++
- C program to find the median of a given list.
- Minimum Cost For Tickets in C++
- Program to find length of longest alternating subsequence from a given list in Python
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Find the tuples containing the given element from a list of tuples in Python
- C++ Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
- Program to find H-Index from a list of citations in C++
- Find kth node from Middle towards Head of a Linked List in C++
- Find pairs with given product in a sorted Doubly Linked List in C++
