 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna do a Depth first search question. So what from now on we're just gonna do depth first search questions mainly because It's better as as I said in the last video It's actually better if you start doing questions that you don't really understand So then when you get used to doing these questions, you'll actually improve instead of just mindlessly doing a bunch of easy's so This is a depth first search question called employee importance Um essentially as you it is another easy, but essentially is your it is a depth first search question so you're given a data structure of the employee information which is the unique ID and his importance value and his direct subordinates ID, so if Employee one is leader of employee two and employee two is a leader of employee three They have importance value fifteen ten and five Then the employee one has a data structure that is one fifteen and then two because two is the Direct subordinate ID, so that's like the one that's under him and employee two has two ten and three because Three is the subordinate of Two yeah because two is a leader of three. So that's why for Employee two three is going to be in the subordinate area Employee three has relation of three five and empty. Yeah, because three is not a leader of anything. So that's why it's empty Okay, so now we have that are the way the Relationship is not direct. So note that employee three is also subordinate employee one the relationship is not direct. Okay, so essentially is that employee three is really a subordinate of employee one, but It's not going to be listed as well. Okay It's not like a direct relation. Okay, so now that the employee information is a company employee ID You need to return the importance total importance value of this employee and all the subordinates So you're given the employee Information of the company Now given the employee information of the company and the employee ID you need to return the total importance a total Importance value of this employee and all the subordinates. Okay, so Let's see what we have here. We have one five two three. So two three is the subordinates of one Five is the importance value. Okay, two three empty. Okay two Yeah, yeah, so there's no subordinate of two as three importance value three three This is empty Three three is the three is the importance value and then you have one. Okay, so one that one means is that we're trying to find the Total importance value of one and all of its employees and all of subordinates. So here in this case It would be five plus three plus three because five one's importance is five and then you have to add up the the importance value of its subordinates was two and three so in two What is it's importance value is three and then two has nothing there So we're not gonna do anything and then remember three is another Subordinate of one. So three we have to add that add that some the importance value of three also into the total sum into total importance value, so I'm gonna do five plus three plus three and that's gonna get you 11. Okay, so What I'm gonna do is I have to go through so here. I'm gonna list this out I haven't done this question out before though. So I'm gonna go through Okay, first first have to go through the employees and Get the ID. So I have to find the right ID, right? So like I have to find one. So once I find that So once I find that once I so once found the right ID We are going to go through each subordinate Each of its subordinates and Whoops and add up all The importance value of each subordinate, okay? Okay, so Okay, so first you have to go through The employees and then get the ID once we find the right ID We're going to go through each of its importance and add up all the points you support. Okay, so Okay, so it's a unique ID of each node Importance, okay So I'm gonna go through every employee. So employees For I was zero I was less than employees that size I plus plus So this is gonna be Current employee I'm searching It's gonna equal employees I So I so now I'm going to We have to get the right ID. So if Current employees ID is equal to ID So if it's equal Then we have to go through each of its subordinates and then so get the importance value of each subordinate and add up the importance of each subordinate so I have to go through Let's see. Yeah, I'm gonna do call this Create a sum called sum total Total importance import zero I'm gonna do sum total import plus equals to Sum total import we're going to add Current employee It's importance value. So Okay Okay, so we have to check if we also have to check if the current employees not know Then do this right because if if he is an all then you can't get the ID. So now you close the null and So I'm gonna put this here Okay So what we're doing here is we are going through each employee This loop is gonna go through each employee. I'm gonna have a sum called total import, which is total importance I'm gonna get the current employee If it's not null so that means it's not empty We're gonna get the ID. We have to check if the ID is the right ID if it is we're gonna add some total of the importance the current importance then what we have to do is we have to go through each subordinate Here's like two or three and then add up theirs as well. So I'm going to check check if current employee Yeah, we have to check if employee is not it's subordinates Is not equal to null so it's not empty Actually, there's a not empty There's an is empty. I think that's way better to use. Yeah, okay. So once we get that we have to go through every single value in coordinates so current employee subordinates dot size Tell me okay, so then All right, okay, so then okay So once we go through The size oh, we don't even have to check this actually because if it's empty then it would be okay So that was a pointless check Because if the side if it's empty the size would be zero. All right, okay, so we're gonna go through every other that that and We are going to Get Gonna add up some total and we're gonna recursively call this method passing in So they have an employees Subordinates, what is what is the oh? Okay, so I have to do the ID of the directed subordinates. So this is two three. So then I have to pass in Employees at two and then employees at three. So I have to do employees at x and Then the ID would be employees at X Oh No, it would just be ID because we're passing in the idea again. Okay, so I think this would work Okay, so let's backtrack here again. Okay, and then I'm going to return some total Okay, so we're gonna go through every single employee In our list You get the current employee If the current employees not know What we're gonna do is we're gonna check if it has the same ID as the idea we're searching for If it does we're gonna sum up the importance of the current Employee from the current employee to the to the total importance Then what we're gonna do is we're gonna go through to each of its subordinates of The current employee and then get the total important get the importance of each Employee so each like two two three Three and then after that we return some total important Alright, so Let's see. I have a feeling this may or may not work, but You have to try. Okay, we have a compiler. No matching calls. We get importance But this isn't important employee at Subordinates, okay, so what we need to do is we need to Employee at zero. Okay recursively get the importance It's a good question. We're passing employees again. Okay, I got a stack overflow. Let's see Okay, so I can't do this. I need to pass in an Employee Then get the importance of that So I want to keep going to each employee and then get its importance So I need to do another method here. So I'm gonna do get importance Helper I'm gonna do an employee Let's see once you have an employee you need to have the subordinates, okay, so current employee and ID that we're finding this is gonna check if current employee if it's Here I'm gonna make this a pointer if it's it's Subordinates if its ID is equal to ID we're finding Then we're going to go through each of its subordinates. So we're gonna do X equals zero X is less than current subordinates That size Then we're going to pass in Okay, so I'm gonna pass in employees into the here actually So then I could pass in employees at X into get importance helper so employees at X and then the ID Let's see. Let's see if this works. So what I'm doing is I'm going to This helper is supposed to keep going down based on the employee Current employee to check if it's the ID of the right employee and it's gonna go to its subordinates right, I can't recursively call this unless I have the actual Total employees that was given because I can't get this like I need to get the second So like once I go through subordinates two and three right I have to get the employee at two so I needed the total I need the whole Employee list in order to get two and three You get it like the employees of two and three to here and here My bad Employees of two here and employees of three That's the reason I need this is because I need to get to That's the reason why I need this employees List so I could get the employees at two and employees at three so then I could get this and that All right, so each of these individual lists Okay, so that's why I pass this in here and that's why I pass this in here, okay So we're gonna get the total list Employees Yeah, I need a sum also in some So then I'm gonna do this some total and I'm gonna do some total plus equal to Current employee at its importance Then after this I'm going to return Some total so essentially this is gonna do exactly what are our function here couldn't do Because I couldn't get the current employee for anything, okay, so Yeah, I'm gonna get rid of this and I'm going to pass in Current employee so I'm gonna call this get importance helper Passing in the current employee and the current ID Here was current employee is gonna be current employee We still need the same ID that we're finding Employees is gonna be the same employees We're gonna do some total import the plus equal to this and then we're gonna return this Okay, this may or may not work. I don't know It's runtime error. Okay, so we still have a stack over flow. Let's see. What's what is the problem here? Okay Okay, so at this point Should find the Go through it subordinates get its ID plus equals of importance return it It's gonna come back here Poise size again. Okay, so I'm gonna what I'm gonna is I'm gonna print out Current employees name or ID. Yeah, the current ID. Let's see what's what's going on I'll just run it for now See Okay, so he has one too. I'll copy this and put as a test case Let's see. What is causing this overflow? Okay, let's see Hold up Um, let me see something. What is causing this? Why can't I go down? It's not letting me go so down Um, it's TD out is one That's strange. Okay one Okay, let's see what we're doing here. So I have the size one two three, right? I have a size of three I'm going through every single Every single employee Get the current employee. Okay, so currently my current employees one I'm print current place not know so I'm going to get importance helper of employees and current employee passing the ID so I'm gonna go here and then I'm gonna check We check the right ID. So is one equal to one? Yes, it is I'm gonna sum up the total which is going to be five Then go through every subordinate size and some of that one. So I'm going to pass in Employees there's a list again employees at X which is going to be two and then three and The ID of two and three so it's going to come back up here and it's going to say is Two equal to one. Oh Hmm Just gonna check if is two equal to one. No, it's not Then that's going to return zero Okay, let's just keep going and see what happens. Okay return zero. It's going to come back down here Okay, let's see. Let's see what we're printing out Let's print out some total. Let's see what happens This is called the debugging skills guys. Okay, so it prints out one five five five five five five five five five five five. Okay Goes back up here Hold up. Let's see. Let's print out this Employees at X I'm gonna print out its ID Wait, no, no Current subordinates should be No, hold up. The subordinates should be two two and three Wait a second Wait a second Two and three You have to go through each subordinates, which is two and three So that's what I'm doing here Two's ID should be I know what I did wrong. Okay, so here is gonna go through the subordinate size, right? Subordinate at X. So it's gonna go so here I went through This list right the subordinates two and three. I Didn't pass in the right value for each subordinate. So two and three it's gonna do it'll pass and get helper employees at zero because it goes through the size of The list two and it's gonna go zero one because it's a list of the size two and Then over here, it's gonna pass in the value zero in here Alright, and then over here once it gets What's the value of zero in this list? That's For this at the end of X. That's this that's the first That's the first Employee so that's why it's gonna go to an infinite stack overflow Okay, so what I should be doing is actually doing current subordinates at X. That's what I should be doing So when it's gonna print that should print out two and three to Okay, so there's a stack overflow because of that issue there, but it prints out two. Okay, so now I have two and three I want to pass in the Value of two and then the value of three To add up those values. Okay, so I'm gonna pass in To I need to get the Current subordinates, so I need to pass in the idea of here current subordinates at X Employees at X. What is that? That's not what I'm trying to get Okay, so I don't need employees Do I need current? So what I need to pass in is the current subordinates and their ID So that's gonna pass in two and three Into my function, then I have to get the employees at X which is not employees at X. I need to Get each current Do I need a helper function actually let's see employees at X Hmm We're gonna go through each employees So one two three check if two and three Okay, this is gonna be a very very this would take a while actually. Let's see how long is this gonna take? Hmm Current subordinates at X Excuse me I'm a little bit sick right now Employees employees at X current employees at subordinates employees employees Hold up. Okay, so what we're gonna do is Hmm, I think this should work. No, let's see employees at X is gonna be zero one and that's how it's gonna stop there This is supposed to go through the the It's supposed to get each subordinate Once I get each subordinate two and three I need to Go to This each employees The employees at two and then the employee at three So I have to find the the right the right The right employee to return So I need to get this employee So nor do that. I need it. I know for them After you That's called y equals zero. Why is less than? Employees This is going to find the the right employee. I feel like there's a much better way to do this But so this is gonna find the right employee. So if employees at y ID is equal to current subordinates at X So this is gonna get the The current ID of two and three and then we're gonna loop back to the whole list and then we're gonna get the If it's the right one, so if this is the right ID I'm gonna have a employee subordinate employee I'm gonna set it to null and then I'm gonna do subordinate employee is going to equal to employees at Y I'm gonna break Once I get that employee, I'm gonna pass it into here So then I'm gonna have sub employee is gonna be this one, okay, and then That should get so that I'm gonna pass. So what this is gonna do is it's gonna get the importance of sub employee Which is gonna be this too? Now pass it back to the front and it's gonna do that again for its children yada yada yada Um, I think this should work now. Let's see Yep, it works. Okay All right, so I'll explain my solution and then we'll look at the answer So what I did first is that I had a for loop to go through each employee, right? I'm gonna get rid of this So I'm gonna get first. I'm gonna get rid of the console logs to the see-out statements. Whoops Okay, so first I did was I haven't I Went through every single employee list in our employees list which is Each individual ones of these so this employee this employee and this employee Right, that's why I did I get each employee. So this current employee Is gonna let's say we're gonna start at zero and then one two. So you're gonna get this one this one and this one I would check if it's Empty right if it's if it's null that means empty. So if it's not null that means it's not empty And I'm gonna get the importance helper of this employee, which is one five two three, right? So I pass in those values into this function call it get importance helper That takes an employee is the current employee in the ID and If the current employees so let's say this is one Current current employees ID One is matches the ID that I'm searching so I am searching is one, right? The ID for searching is one Then I'm gonna do is I'm gonna sum up the importance which is five. So I take five five is important I'm gonna add it add it up with some total then I'm gonna go through every subordinate which is two three Right, so this this loop for loop goes through every subordinate size, which is two three I'm gonna get each subordinate. So what I have to do is I have to get the employee at two So I did was I'm gonna loop through again through the employees size Which is one two and three Right, if it has a matching value of two as its ID I break So if the ID matches with the subordinate ID right here The whatever current employees ID matches with subordinate ID I break and I set my I get my actual Subordinate employee, which is this would be two and then I recursively call on that So I'm gonna recursively call on two and then it's gonna sum up the total of three empty Then it's gonna come back up here, and then it's gonna recursively sum up three, which is three for a three's employee and then yeah, that's what it does and Then that's how you get That's how I got AC, but it's not as fast. So let's see at the actual solution Okay. Wow, this is much easier Okay, so They use a Hash set a map Genius. Oh, okay. That's actually genius. Okay. This is actually really smart. Okay. If you use a map What there is gonna do is they're gonna to use it to query the employees quickly so they're gonna map So we want to find the total importance of each employee It'll be the importance of that plus the total importance of each of the employees subordinates. This is straightforward that first approach Okay, so they go through Okay, so they create a new map and For each employee They're gonna put the ID. They're gonna map the ID with the current employee And then they're gonna call depth first search on the The ID we're searching Okay The ID we're searching they're going to get the employee at the specific ID They're gonna get its importance and they're gonna go through every of its subordinates and then add The ID. Oh, okay. It's gonna add the depth first search They're gonna call that first search on that ID. Okay, so that's actually much better than our Solution so what our solution did was that every time we What we did was we had to go through every single Employee every time to search for the right sub employee here So that's what we had to do here Once we found we have to break and then we had to pass that in Here they don't have to do that because if you use a hash map or a map or whatever it is You don't have to keep on Searching to find the employee of the sub employee the idea of a sub employee So in in this case two three, they don't have to search for that the employee that has the ID to For like this one too and then add its importance. They could just get it from a map So if they map every single ID to its specific Employee so they're not they don't have to do the searching So that's actually way smarter Because if they don't have to search through it, then it's much easier and it doesn't take that much time So we're gonna actually change our solution to match theirs. Okay, so let's see what we have to do First of all, we're gonna create a global variable as a map It's gonna map an employee to its We're not gonna map an employee we have to map the pointer of an employee because we're using C++ No, we're gonna map the specific integer ID with its employee And this is gonna be the Total map Okay So this is gonna map the ID to its employee. Okay, so what we're gonna do is In our function here. Okay, let's not look at the solution right now Okay, because we in order to learn you cannot copy the solution. Okay Okay, so what we're gonna do we don't need this for now And we don't need this and Let's get rid of our helper for now Okay, I'm gonna call depth first search Okay, that's gonna be our helper First we're gonna map every single Total map Every single ID so employees at let's see what is the Okay, so I'm gonna okay first first I got to get each employee So current employee is gonna equal to employees at I Okay, we're going to map each employee It's ID to its specific Employee so this employee. Okay Once this is done, we're gonna call depth first search passing in the Turned up first search Passing in the ID we're finding Okay Okay, so in depth first search, we're going to have this ID and this is gonna return the total sum What we're gonna do. Okay so Once we map the correct ID We have to get each Subordinate For the the subordinate ID So Wait, well, let's see what they did. Yeah, okay, so you have to get each You have to get the employee that has the ID Okay, okay. Oh my god. Okay Too much looking there. Okay, so we have to get the Employee that has the ID so this is the ID So this is the employee that has the ID then we're going to Add up its importance. So in this case total import We're gonna add up the importance Importance. Yeah, once you add up the importance you have to go through each in the go through Let's use a for each loop. Okay for every integer s so for every subordinate inside Currents subordinates we have to add the depth first search of its ID so The ID of s Yeah Okay Then we return total importance Let's run that code and it gets accepted Okay, so that's how you would do this question for depth first search for employee importance. I hope you guys enjoyed My tutorial. I didn't do this question. I was just doing it with you guys So this video would have been is very long, but I hope you guys enjoyed me doing this question with you guys It's actually much better if I do a problem that I've never seen before instead of just like explaining the solution So I'll explain it one more time We're gonna go through every single employee and we're gonna map its ID In our hash map to the current employee that has so here it's gonna be one One is gonna have the ID The it goes through this one two and three so each ID one is gonna have this This employee right this whole employee to two's ID is gonna have this employee and three's ID is gonna have this employee Right once you map that we're gonna call depth first search on the ID. We're searching which is one so once We have the ID of one So we pass in one in here that we're trying to search for we're gonna get the current ID of the current employee from the ID one So this would be this one It's employee. We're gonna add up in importance, which is five and Then for each Subordinate ID two and three. We're going to recursively call depth first search again So it's gonna go to the employee that has ID two, which is this employee And then it's gonna add up those The total importance of this one is three And then it's gonna return that because there's no subordinate here so it's gonna plus three and Then so it's gonna do five plus three and then we're gonna get the next next Subordinate, which is three and then we pass this in here. We get the The employee of three and then you add that and you can add then you have five three plus three Which is 11 and then you return that and that's what you get So that's how you do this problem rate comp subscribe. This is a very long tutorial