• 2 days ago
Transcript
00:00Next is to find itinerary of
00:30Itinerary means journey
00:33I am going to tell you where to start, where to start, where to fly, where to start, where to
00:43start, where to start, where to start, where to start, where to start, where to start, where to start, where
00:43to start.
00:49Here, the question of itinerary, the question of tickets is usually we read the graphs of the chapter. If you
00:57see such a question, it will come to your mind that it should be done by the graph. Why? Because
01:01usually the graphs are such a question, where one point is connected to another point. We can visualize the points
01:07here, Delhi, Chennai, Goa, Mumbai, all the graphs are made of notes.
01:26we can solve this question. How can we solve this question? Topological sort name.
01:35Topological sort is a fixed algorithm which is used to solve. It's a journey question where you have to get
01:41the whole journey. But we haven't learned graphs yet. What have we learned? Maps and sets. So, we will solve
01:47this question using maps and sets. Time will be enough. It is an optimized approach which we are going to
01:53solve. And with graphs, in fact, we need to create nodes, we need extra time, a little extra effort.
02:00Time extra doesn't look like it, it looks like extra time. So, what do we do? This question is going
02:04to be maps. In the question, if I go to Chennai, then I have one ticket which goes to Chennai
02:11to Bengaluru. The second ticket which goes to Mumbai, goes to Delhi. The third ticket which goes to Goa, goes
02:19to Chennai. The fourth ticket which goes to Delhi.
02:25I can start from Mumbai. Why will I start from Mumbai? Because Mumbai alone is such a point where we
02:35start, where we don't come. A ticket is from and two pairs.
02:43where are we coming from and where are we coming from? Chennai, Bangaluru will come from Mumbai and Delhi will
02:49come from this way. But Mumbai is only a place where we don't come from anywhere. So, I have made
02:55a graph here that I have to start from Mumbai. Why? Because if I started from Goa then I will
03:03never come from Mumbai and Delhi. I will never come from Bangaluru and then I will never come from Mumbai.
03:08I will start from Mumbai. After Mumbai, I have a ticket from Delhi.
03:37from or two pairs
03:40What do I want to take out? First of all, the most important thing to me is to take out
03:45what will be my starting point. If I have my starting point, I can calculate my entire journey. I mean,
03:53if I know in this graph, I want to start from Mumbai. Then I know, where is Mumbai? Delhi. Next
04:00I will go to Delhi. Then I will go to Delhi. Then I will go to Delhi. Then I will
04:04go to Delhi. Then I will go to Goa. Then I will go to Goa. Then I will go to
04:06Goa. Then I will go to Goa. Then I will go to Chennai. Then I will go to Chennai. Then
04:08I will go to Chennai. Then I will go to Chennai.
04:10Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore, Bangalore.
04:40If I talk to those people, where I have a ticket, and I talk to those airports, where I have
04:46a ticket. So what will come from from? Chennai will come from, because Chennai has a ticket from Bangalore. Mumbai
04:54has a ticket from Delhi, which means Mumbai has a ticket from Delhi. Goa has a ticket from Chennai, which
04:59means Goa is from Chennai. Delhi has a ticket from Goa.
05:05Now, which place does not exist within 2? Does Chennai exist within 2? Does Mumbai exist within 2? Does Mumbai
05:14exist within 2? No. So, what do I do? I have stored data from and to and now I am
05:22checking only for this 2. I don't need to check only for this 2.
05:27If I checked for two, I will check for Chennai. Chennai exists, Chennai exists, then I have checked for Goa,
05:35then Goa exists, Delhi exists, Delhi exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai
05:43exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai exists, Mumbai
05:47exists, Mumbai exists,
05:55I will take one data from which I will store from from which I will store from one data from
06:02which I will store from. I have to get two maps. This is my one map and this will be
06:08my reverse map. So in one map, my data is already there.
06:15In the beginning, I assume that this data is already given me and if it is not given, let's say
06:22it is 2D array, so I can convert it in the map form. This will be my from part and
06:27this will be my to part. I already given that my cities are unique. I mean, it won't be like
06:33Chennai from Bangalore. I have no case. What do I do? I have to go from from to to to
06:40to to.
06:41So I have my map. So here I have already stored this data. Now I have to store reverse data
06:46from from from from from from from from from from from from from from from from from from from from
06:54from from from from from from from from from from from from from from from from from from from from
06:57from from from from from from from from from from from from from from from from from from from from
06:58from from from
07:06So here Goh will come and here Delhi will come. Now what is my work? I have a starting point.
07:11Starting point is such a thing which is in from but not in to to. So I will match from
07:17to to to to to to to from from from from from from from from from from from from from
07:20from from from from from from from from from from from from from from from from from from from from
07:23from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from from
07:27from from from from from from from from from from from from from from from from from from from
07:31First of all, I picked Chennai, this key. For this key, what does this key exist in two? It doesn't
07:37exist. So, Chennai is not starting point. Then what did I do? I picked Mumbai key. Mumbai key exists in
07:45my map from, but my two doesn't exist. So, my starting point will be here, which is equal to Mumbai.
07:56The most tricky part of my question is how to solve two maps. In one map there is original data
08:02from from from from from from from from from from from from from from from from from from from from
08:08from from from from from from from from from from from from from from from from from from from from
08:10from
08:18find out. As I got my starting point, I mean, I got Mumbai. Then, what do I have to do?
08:24Now, I have to see that my original map, where did I go from Mumbai? Where did I go from
08:31Mumbai? Where did I go from Mumbai? The value will come from Delhi. Now, what will I become? Delhi will
08:35become my starting point. As I become my starting point, I will go from Delhi. So, I will go from
08:42Goa to Delhi. So, what will I become my starting point?
08:45Goa became Goa. Now I am going to go to Chennai. Where is Goa? Now my starting point became Chennai.
08:53Now I am going to go to Chennai. After Chennai, my starting point became Bangaluru. I am going to go
09:01to Bangaluru. I am going to go to Bangaluru. I am not going to go to Bangaluru because it doesn't
09:05exist. Now I will go out of this loop. My itinerary, my journey, what happened? First I went to Mumbai,
09:11then Delhi, then Goa, then Chennai, then Bangaluru.
09:15So I used to use my original map. Why did I use this reverse map? Just to find out my
09:19starting point. Once I got a starting point, then I used to use my original map. And we can take
09:25a journey with linear time complexity, with the help of hash maps. In my main function, all my tickets exist
09:35already in the form of hash map. If you have given a string of 2D array or some other structure,
09:41array of array.
09:43Array List of Array List of Array List of Array List of Array List of Array List of Array List
09:44of Array List of Array List of Array List of Array List of Array List of Array List of Array
09:46List of Array List of Array List of Array List of Array List of Array List of Array List
10:13The starting point will be long and give it a name of getStart. Under it, I am going to pass
10:17my hashmap. And what will I do? With this hashmap's help, I will return my starting point and give it
10:24a string type. That means that city where I want to start. First, I will create a hashmap which will
10:30store the data in reverse. It will store the string of string. But this will be my reverse map is
10:37equal to new hashmap.
10:39before from from from from from from from from from from from from from
11:04I have written a little syntax on my hash map. Now, the questions of hash map are small. There is
11:12no problem in understanding. There is a small thing that I have to remember the functions. How do Iteration, remove,
11:19add, get, contains, check. If I remember the functions I have discussed, then I will solve the questions in hash
11:26set. Then I will just think of my logic and implement it. So, how do I directly identify the hash
11:34map?
11:34Iterate Iterate I will remove the key set for tickets for the map and I will run a loop with
11:40string key. So every time the string will store my Iterator in my Iterator. Now I have to do what
11:47I have to do. The key will become value and the value will become key. So from 2 will become
11:54to from 2. So I will reverse the data in reverse map. Now the key will become value. So I
12:02will write tickets here.
12:04.get key and where value key comes, I will simply write key. In this way, I will create my reverse
12:10map. Here I will write .put. I have passed my value as the new key and key as the new
12:16value. Now, what do I do? How do I get starting? Starting is the one that exists as key but
12:24exists in reverse map. So, I will run a loop on my tickets again. If my reverse map
12:58So this key will be my starting point.
13:04and I will return it directly from here. And if there is no starting point exists, then I will return
13:09it null. But this function, I have just said to remove syntax error. The answer of our solution will be
13:16our test cases. It will be in this way that there will never come that the starting point exists. If
13:21I want to assume this, then I have returned it directly from here. So here I can call the function.
13:29String start is equal to get start which I will pass my tickets.
13:34So this function will return me starting point. Now I have to run a loop. For. Now I will run
13:42a loop on my tickets. String key for my tickets dot keyset. What do I do every time? I will
13:54print in the starting point. So I have to print my start here. Now what do I do every time?
14:03I know that ticket is here. So every time I will print.
14:10MyTickets.GetStart and I will add an arrow for clarity. What happened here? The starting point has come to me.
14:23Now, where is the starting point? I will do Tickets.Get. What is the starting point? What is the starting
14:28point? From. From to to, what will happen? So, Tickets.Get will come from the start value. I will print
14:34that value in my next city. Now, start to update the start. Tickets.GetStart
14:42This means the next city where I am going to come, it will become my starting point. Now, I will
14:47remove next for that. In last, I can print my lake next line. Save and one more run. The output
14:56that I have come, I will start with Mumbai, then Delhi, then Goa, then Chennai, then Bangalore. And this is
15:02my linear time operation. I also have linear time. I also have linear time. I also have linear time. So,
15:09I have solved this using big of end time complexity.