 Alright guys, Mike the coder here. We're gonna do another lead code problem. I'm noticed in my other vid that I Have literally been screaming too much in the microphone, and that's not it's not good because what happens as it gets clipped But we're gonna do another Google question, and this is called evaluate reverse Polish Polish notation Okay, so evaluate the value of a arithmetic expression in reverse in reverse Polish notation man, that is a mouthful So the valid operations are plus minus multiplication and division and each operand may be an integer or another expression Okay, so what they mean is that let's say we have this This is this example here, and I'll actually pull up paint real quick We're gonna go on this Google grind Google grind set. I'm in the Google grind set right now if that makes any sense Tomorrow there's gonna be a code forces contest, so I'm gonna be prepared for that also I have not done the ad coder in a while, so I might also do that also But yeah, I also haven't done code code chef in a while, so I Just got a book called a competitive programming for also, so I'm gonna go through that It's gonna come in like a week though, so Anyway, let's go through this example problem, and we'll see how this works. Okay, so We're given this tokens of two one plus three and then multiply and our output is nine and the reason why is because Two plus one times three is equal to nine So essentially what they did is what they took this plus sign here The first plus sign they saw and they applied it to both of these expressions of two and one So that's why they got two plus one. Okay, then once they have this number They took the next sign which is multiplication, and they applied it with With three so they took once you have three you apply this multiplication sign to here multiply by three and you get nine Okay, so that's the first example So the tokens are like an array of like strings by the way, okay? So the second one is a little different, so we have four thirteen five Division plus sign so essentially what they did was They went through here, and then they found this division sign here And they just applied this division sign to the two previous numbers of three and five thirteen and five So that's why they have thirteen and five here then once they evaluate thirteen over five they took this plus sign here from all the way at the end they applied it with the result of Thirteen divided by five and they use that with four so that's why they get four plus thirteen divided by five And it gives us six, okay Okay, and then the third example Yeah, so a little tricky, but it is what is so we have ten six nine three Plus minus eleven times division multiply seventeen plus five plus, okay? So they go through here, and they find the first first Operation sign so this plus sign here, and they're just going to apply this plus sign with this nine and three, okay? three So that's where they get this that's where this to get this from then what they do is they take the next sign which is this Multiply multiplication sign, and they use it with the result of nine plus three here, and they apply it with Minus eleven so minus eleven get to put down here, and that's where they get this multiplication part, okay? then they take this division sign here this division and they just apply it to the result of The last number that they saw which is six So after applying all those values, they took the next operation to apply it to the last result They had which are six so then you have six divided by whatever result that we had before and then once you have We go the next operation multiplication They just take this and apply it to the results of what we had before and then the previous number which was ten here Okay, and then after that That's going to give you like this giant result here, which is I believe This ten plus ten times six divided by negative one thirty two, okay, and then once they have that they take this 17 And then they have the whole result from this what we had earlier here And then they apply the addition sign to both of these two numbers that we have here So essentially what they're doing is that they're taking The next sign that they had like whatever operation sign that was In applying it to the previous two numbers that they saw beforehand Okay, and then yeah, so then after we have this huge result We get the next sign which is this plus sign and then we apply it again with the result of All these values here, okay, and then in the end they get whatever the number was 22, okay, so that's what that's the explanation Okay, so how do you do this problem? Most simple way to do it is actually using a stack So what we're gonna do is we're gonna just like push The previous two numbers onto the stack, so we're gonna have like one stack here right Let's go just call it like our stack Okay, and then we're gonna push the first two values here So like while looping through our this array we're gonna push like two Yeah, so if it's a number if it's a number we're gonna push the number onto our stack So we're gonna push two and then we push one to push one. Okay, so then Okay, so then yeah as we're looping through all the values here we push two and push one If we seize an operation here, that's not a number like a plus sign division sign multiplication sign subtraction sign I think well, yeah, I think I think that's all it yeah plus minus multiplication division What we're gonna do is we're gonna take the pop Two of the values out of our set our stack. So we have to make sure our stack is like not empty by the way So we're gonna pop the two values. So pop one pop two And what we're gonna do is we're gonna just apply the operation whatever operation that we're currently on So we'll do like one plus two because the plus sign is the operation that we currently on and then Let's just apply this operation. So then what's the value with the expression one plus two will give us three And then what we're gonna do is we're just gonna push three our result of three onto our stack So we push three here. Okay And yeah, we push three here and then then that's that's it, right? So then this operation is done and we'll go to the next number So here now we have another number three So let's just push that onto our stack because it's a number. So we're just gonna push three onto our stack and Then yeah, and then afterwards We go to the next value, which is multiplication So what is multiplication multiplication is not a number So what we're gonna do is we're just gonna pop the the previous two values off of our stack So we pop three pop three and we're just gonna multiply. We're gonna use this multiplication sign on the three two numbers Three times you give us nine and then yeah, then after that's done We just push the nine onto our stack Which will give us nine Yeah, and then In the end our stack is just gonna have one number nine and then we could just return that number. Okay Yeah, that's the gist of this code and it's not that not that difficult I'll just show you guys a code now After explaining the algorithm. Okay, so we have our stack, which is just a stack of integers And what we're gonna do is we're just gonna loop through all the Strings in our tokens so that all the values in our array Here what I do is I check if it's a digit So if it's a digit or like a number or something or like this current size is greater than one So this current size greater than one is just seeing if it's like maybe it's a two-digit number or three-digit number Right if it's any kind of digit, right? What I'm gonna do is I'm just gonna convert it into a number Convert the string into a number and push it onto our stack and the reason why I do this is because I don't want to I don't want to push like if it's like a Operation like multiplication division subtraction Edition, I don't want to push that onto my stack, right? I just want numbers So that's what this if condition is checking for it's checking if it's a digit or it's Two-digit or three-digit or four-digit number, right? It'll just check like one of those, okay? Okay, so we push the current number onto our stack All right now otherwise this L statement is in the case when we're actually looking at a operation like a plus sign the division sign Subtraction sign multiplication sign addition sign So in this case what we do is we just take the two numbers from Off of our stack. So here I take the top part Our stack top so you get number two first and I get number one. It really doesn't matter in C++ what happens is that the Yeah, what happens is that the values that you have When you get do top it doesn't actually pop it doesn't return anything So you have to actually get the top value and then pop it I know it's kind of weird in Java and I think in Python It's different where if you do a pop it actually returns the value But in C++ it doesn't do that. So yeah, so now we have number two and the number one Okay Then I have a result variable which is just going to do the operation of Our result. So if the current string is a plus sign, I just add number one plus number two If the current string is a subtraction sign I subtract one and subtract number two If the current string is multiplication sign, I multiply number one and number two and if the current string is division sign I divide number one and number two Okay So yeah, I believe yeah, and the reason why I do two first and then one first is because you need to do It's always like the second number is like the last value that we just pushed right So that's why it's like if I push two and then I push one one would be the second number that I'm plying it Right like if I if we go back to our thing If I push two and I push one Like if I'm looping through this and I push two first and I push one one is the second number that we were looking at So we actually have to apply the operation onto two first on the The first number of two first and then plus the one. Okay. Yeah, that's just how stacks work So, yeah, if the strings any of these values just Apply it the result and in the end we're just going to push the result back onto our stack Okay, and at the end of the return statement We just return the top value which is our stack top top because that in the end We'll have like one number left and that'll be the result of our answer and we just return that okay So yeah that we return the top value our stack top and that's the last number on our stack And yeah, you guys wanted me to run this so I'll just run it And it gets a seed. Okay, so yeah, that's pretty much the gist of this code There's not much else to it in this problem. Hope you guys are there. I hope you guys enjoyed this video very calm subscribe I'll check you guys later. Peace