video_id
stringlengths
11
11
content
stringlengths
0
123k
datetime
stringlengths
19
19
leetcode_number
int64
1
2k
thumbnail
stringlengths
48
151
title
stringlengths
9
106
XnY8fe0Gs0E
it has been asked by Facebook Tik Tok Amazon lyt Google yendex Snapchat nagaro Amazon lyt Google yendex Snapchat nagaro Adobe Airbnb LinkedIn Microsoft Apple Adobe Airbnb LinkedIn Microsoft Apple Spotify Uber we can have let's quickly see what the problem says the problem is simply short and simple saying that we have two strings s and D of length M and N okay respectively uh just one thing in the future code you will see that I represent my string n with length M and string t with the length M there's just a small change here so just remember that and return the minimum Windows substring of s return the again substring is a part of a string window is nothing but same saying okay it's a substring of s such that every character in t including duplicates which means if in T I have duplicate characters let's say a a b c a a is coming duplicate a is coming twice so in the window of s which I will take my a should also come in twice so okay and including the duplicates is included in the window and again in that window portion which I have I can have a a B I can have other characters also d c it's just that in the window all the characters of D should come in that window of s that's the only condition and all such Windows okay all such possible windows I have to minimize the length of that window and return that specific window which means return that minimum length window having all characters having all characters of T in that specific window now just says that there will not be any two windows having same so basically cases which are generated the answer is always unique which means there cannot be two windows where the window length is minimum but you have two different strings that's not required that's not uh given and also if it was given that it would have specifically mentioned that you can return any window but yeah uh for example for example for in this case you will see that this is one of the window in which you have corrector a corrector B Cor C and this is my condition which means all the correctors again duplicates also it should be inside my back window so right now the length is six but I have to minimize my length so I will try to find some other window so some other window is this specific window again I have a b a and c a b c order doesn't matter I just only want to have all these characters inside it now this length is four so okay I can minimize my length this is the minimum length and just return that specific minimum length window that is B A and C is the answer now uh first very basic boot Force approach which comes in our mind is AR and I have to try on for all the possible substrings and in that okay which means we will try on as you can see a substring is okay you will try on for this string then this then this then this which means you are trying on for all the possible substrings now number of substrings in a string of length n again remember I said that my string s is of length n and string T is of length M just a small change uh so for sure to just figure out all the possible substrings I will have o of like n Square substrings so o of n Square will for short com in picture now when I have figured out a specific substring now I will have to check if all the characters of p is actually lying inside my that substring or not so one very naive way for this specific check to just check okay all the characters of my t is lying inside my that window that string which I have formed because I know I have n square substrings but yeah one to to just have a check okay that specific substring that specific one subring is actually good for me or not and it will be good only when the characters of T is lying all characters of T is lying inside that substring so very na check will be I'll just go on and check okay if if that character of T is inside my window or Not That C of is inside my window or not and all that stuff so for sure I will have to iterate on my entire for every of my sub strings so that will be o of n sare into M but you have so far thought of I why have to you go on to every character again and again for T just make sure that you keep track of all the characters of T in a hash map because you know that you have only English alphabets so you will have only 26 characters so now for that specific window just make sure the just make sure that okay you have just 26 characters frequency so just make sure that all these characters which are here for t for T should lie inside that specific substring so I can just simply rather than iterating again and again on the entire string T I can keep track of the count of frequency of the characters of T and then use those characters so I will reduce that to just over 26 time rather thaning on all the characters again and again for the entire and I just for one subring I just have to iterate on the 26 CEST to know what's the Frequency inside my T and would not have to go on the entire T again and again so that can reduce our time a bit but still it is still high it is still o of n squ so right now we have see okay from o of n square into M then o of n square is the complexity which we have seen so far but but but um why why why you were finding all the substrings but if you remember that okay you just have to find a substring with the minimum length so if you have found a substring then why can't you expand or drink that substring why you have to go and try to find the subing again and again now again the main two hints in this problem is that you have to go and find a window you have to minimize or maximize that specific window length window length window and subing both are same window length which means if I have found a specific length then it's good no I will just try to minimize this length that's it it that's it so that is where our sliding window comes in picture when you know okay you have a window then you have to just maximize and minimize that window again in sliding window also there are two approaches that the window will either keep on shifting again the window will always keep on shifting but the window will either will only and only expand and that's it window window will only only expand but they can be Cas Okay window will extend extend extend and then expand expand like like as in string string string expand expand expand shing shing string so there are two variations in which only the shrinking will take place like the size will never ever ever decrease like much Beyond it or never increase much Beyond it and okay in other case it can be increasing decreasing increasing anyway we'll see uh what and how something can be useful but right now we just know one thing okay if we have again if you don't know that how window and all that work no worries hold on we'll just see everything so let's see if we have seen again R what does this window mean all that stuff I'm new to it no worries uh even if you're you you are new and you want to practice just go and watch this arraying playlist in in this we have two pointers window very deep questions very deep now let's say you have a window in you will just have to make sure you have to expand that window or Shrink the window again there's a simple rule that if your window is not good for sure try for a bigger window if your window let's say if you want to S outside let's say if you have a home and if you want to take outside if your window is small your your main purpose is sneaking outside if your window is small then you will try to expand that window so that you can sneak outside right your main purpose is sneaking outside you expand the window what if the window has become large but okay that's taking a a lot of space but you want to minimize the minimize the window length so you'll try to shrink it that you will try to shrink it only up to the point that still you can peek outside that's it so I'll do the same thing right now I can not peek outside when I like when I say peek outside I mean okay I have a and b now A and B C is still missing so I should expand my window size so I just simply expand my window size but AR it will keep on expanding no as soon as it will go up till c now I have got all the correctors okay so when I've got all the correctors this is one of my good possible window now when you have got a good possible window just try to shrink the window and see if it is still good or not so I'll just shrink the window now shrink the window now oh as soon as I shrink it became bad now if something becomes bad oh so okay again try expanding it in the future try expanding try expanding try expanding try expanding and so on and so forth so this is the entire concept of sliding window now just remember two points for sliding window that again that's a standard ad hog template for you to always Implement sliding window like this it will be very busy else you will be like me okay just worried okay where the point is going all that stuff just remember these two points that you will have your I and J J is moving always ahead at every step at every step my J will move j will move at every step that's it no matter what no matter what my J will move at every step J will never stop which is the endp pointer and when I have got a correct window size when I have got a correct window size then then then then which means it matches the condition again the window size logic if the window is correct or not it will depend from question to question right but depending upon if the window is right now correct this I to J window is right now correct then just try shrinking the window and see that if you can minimize the length or not minimize the length or not so if the current window it matches with the condition whatever condition can be in this case the condition is to T then you just try keep shrinking your window now shrinking again shrinking only happens J Will for short move as I told you earlier also J will for sure move and it will move at every step but I move when I have a correct window and and I want just okay minimize the length minimize the length now uh we will see that how it will actually happen now the condition okay for J to move at every step it is as simple as that you will have a simple Pointer while J is less than n and simply J will keep on moving at every step no matter what so this is a simple template for J but what about the condition so for the condition it is simple that right now you are at I andj you have this specific window this is was this was your team now I said that if the current window satisfies the condition now the condition is condition to T so the condition right now is that the I toj window size I toj window size all the characters of T should be inside that window size should be inside that window now to implement this specific logic to to implement this condition to implement this condition only we have three possible options again in the beginning we saw two variations of Brute Force itself now for the optimal which means for the sliding window again we have three possible options one is a very one is a very naive way that okay you have a window I2 J and you want to know if all the characters again we had seen so far is that we will just keep a track of all the characters of T in a hash map of size 26 which will keep try of all the characters of T so that I don't have to on T again and again I just only itate on my window right now so one naive way to check if all the 26 characters frequencies is corresponding to that window of from I to J is one n way simply trade from I to J and check if all the characters frequencies of T is lying inside my i2j window size so it will take a o of n o of n time because I to J is kind of a string s of length n and space will be o one because of no extra space you are using You're simply trading and the hashmap which you had already made of T you you can simply decrease its frequency and find if it becomes zero for all the characters of all the characters of T then I am good now you can see it taking o of end time and that is for every window now window as in okay J was keep on moving so I know one thing for sure J will keep on moving J will for sure keep on moving so o of n will for sure come in picture and then I will also move so o of 2 into n will be my pointer movements and for every pointer movement I am saying I will take o of n time so it's still L of n Square that's bad now the next smart way is that you have to just know again you you have known that the frequency of all the characters int right now you just have to make sure if all these characters are lying inside your I to J that's it so which means ultimately you're also concerned about what are the frequency of characters in the window I toj so when I ask you in the smallest possible time you have to know the frequency of characters basically you have to know the summation in a range of characters of the frequency of characters like frequency okay is it's nothing but the summation of how many times it is occurring if you want to know that if you want to know that simply use a prefix sum so what I will do is I will use the prefix sum to know that I to J to know that I to J what is the frequency of all the characters now if you had not known how we do a prefix sum for these characters so let's say if I have a a b c d and e let's say a b c a b c a A and B so I will just keep track of the prefix sum prefix sum will say a equal to 1 here it will say AAL to 1 b equal to 1 AAL 1 b = 1 Cal 1 a = 2 b = 1 c = 1 a = 2 b = b = 2 Cal 1 now if I ask you what is this specific what is this specific range frequency so you can just subtract this from this so you will get a = 1 b = 1 and Cal to 1 this is a frequency in this range so you sol saw that you just keep track of the 26 characters and length was n so now you know that the time required will be a o of one operation to know a window to to know all the characters in that window and when you have got all the Cs in that window then you just have 26 size on which you have to iterate so time will be o of 26 after you have made this which means the precomputation which you will do will take o of n 26 time this is a pre competition which you will do or you can say of end time that's it uh after this pre competition is done then you can simply go and find any window frequency any window cus frequencies and for sure you can see the space required will be n n is for this entire length and 26 is for 26 is in the worst possible case so that seems okay that seems okay that for sure work again this approach will for sure work in this problem because it's a easy the the the time required is less but we can still optimize the space I don't think we require the space how we know that again now to optimize this space it's actually a bit tricky part how that other ways rather than rather than making the entire new making that because see right now this space is required because I am keeping track of the frequency of s which means all the characters in a range for S I'm trying to find now I'm saying my only concern my only concern was if my T if my character like frequency of characters of T is inside my S or not that's it so rather than making s why can't I simply use my T itself which means I can simply decrease or increase the frequency of characters and T so as to manage my window size so what I thought of okay I can simply use the the the map basically the map of 2 which I made for t i can simply use that map or the frequency of 26 characters of my te which I have made and I simply use that itself to know if my window is right now correct or not and this is actually a tricky thing to optimize the space in this particular problem but many people just directly jump onto it without even knowing that the most intuitive approach is using the perix sum to know the frequency or basically to know the the count again in a in a context if I would have been there I would have used this specific approach but it's more intuitive but still we can optimize the space and we'll only use the T frequency which we have made and thus we will again do it in o one time same was here itself but here you will see again you will see that here the time will be exactly over one like it will not be over 26 like for every step and space will be just as you can see the the T the temp so the the t uh map which you have made the map for T frequency map which you have made that space is the only space required no space of 26 required and that that's how you can simply solve it now now this is actually a tricky part now let's see that how we will solve it via actually using the map of T So as we saw that for a specific string T we will simply make the map of him it is T map so I have written my t- map it is not temp it is a t- map so this is a t- map which I have made now A B C I just keep track of the frequency of those characters as I wanted from the very beginning I will for sure have taken that but now I'm saying I only only use this I'll only use this T map I'll only use this T map to actually trade on and find the window if it is correct or not so for sure in a specific window how you move on okay you have your IJ in the very beginning make sure remember two steps J will always keep on moving I will only move when you have got the correct window now the correct window is all the characters of T should be in that window now uh I seems nice ni um so one thing is for sure to know to know if I have got all the characters in the window how will I make sure because okay if you are saying that I will use this specific map so for sure as I will as I'm standing on this specific character so I said okay I'll use this character when I say I use the character so what modifications I should do in my existing map because I have I know right now that all the modifications I have to do I have to do this existing map only so I'll do all the modifications here itself so one operation I can think of very like very like naively is I am at a this was my existing map I have to somehow make sure that I have taken my a I have taken my a this is the existing map so I simply reduce the frequency of a one 1 a taken 1 a taken then I'll simply move on but as you have seen that 1 a is taken 1 a is taken oh 1 a is taken now how will you make sure if all the characters of of T are taken so one thing you can also make sure is keep track of the initial count of characters in your t as soon as you take any character which was inside your T then you can decrease its frequency or decrease its count okay right now the remaining characters which I have not taken in my window are two this represents that and because as you can see a is taken so a is right now in my window remaining characters which are not taken in my window right now are two and this will help us make sure is if I have if in the current window if I have taken all the elements of t or not because as soon as my count becomes zero as soon as my count becomes zero I have taken all the elements of character I I have taken all the characters of T in my window I to J and then I can simply start simply start shrinking my window okay let's see as you can see my J moved forward again simply decrease again the operation is still same but simply decrease the frequency but R and B is not even there no worries no worries at all D is not there simply decrease the frequency d isus one so if I decrease the frequency so shall I decrease the count also n why not because you know that you because you know your D is not even there now AR that is okay D is not even there but how will I figure out if my D was here or not because you just told that okay as soon as you encounter your nums of J simply go in the map and decrease the frequency okay you you decrease the frequency but then you also said that okay nums of a when the frequency was decreased but then you you you also decrease the count how will you make sure that some decreasing which you doing right now is from your like from your T string or not as simple as that how I'll tell you because because if you have if you have these T if you have the characters of T by default all of them were actually positive which means more than zero their count by default would have been more than zero so if some count is more than zero which means it was it was from my string T if it is less than zero or if it is zero or less than zero because T you can see was Zero T's frequency sorry D's frequency D's frequency was zero so it was not even there in my map so I can just make sure when I'm about or when I when I when I'm about to decrease something when I'm about to decrease a frequency if the frequency if the frequency is positive which means it is for sure from my string T if it is zero H simply decrease it because it is not from my string T so that's how I simply decrease the frequency of D now it became minus one but my count never decreased okay cool no worries move ahead now I am at o again decrease the frequency of O count will still remain same because it is not inside my string T now again increase the frequency again move the I sorry move the J because see you will keep on moving J at every step as you saw J will keep on moving at every step no matter what now uh B B oh B I try to decrease the frequency but before decreasing frequency I can see it its frequency is positive which means it was inside my string T so I'll just decrease the count now my count will become a one I'll simply decrease the frequency frequency of b b will actually decrease and will become a zero and does I'll simply keep on going so right now you have seen that the standard template was while your J is less than n simply keep on increasing your J but I saw that as I'm currently at the GTH index I will firstly check if my t map of that s of j s ofj was B your this character was a b the T map gives me the frequency of that character at the J index if that is positive which means it is lying inside my string T if it is please simply decrease the count decrease the count as you can see simply decrease the count now when you have decrease the count no matter what no matter what you have done this or not simply for that s ofj in the T map decrease the frequency so as you can see the this will happen no matter what for B for a for d for o for everything it will happen but this thing will only happen when my frequency is more than zero more than zero represents represents that it is inside my string T and what makes this question hard rather than other sliding window problem is this specific optimization of only and only using your this specific T map okay and simply this for a step one operation which means simply keep on moving your J forward always now with the stand template we have seen so far okay simply go on moving your T moving your J okay J will move e was not there simply e frequency again minus one now J again moved so C Okay C is C is there frequency was one so simply firstly decrease the count count becomes a zero decrease the frequency of c c frequency will will will become a zero and then J should move ahead but but but ah now do you see one thing do you see one thing it's simple that your count has become a zero your count has become a zero H that is the case which means you know that this specific window has all the characters of your team this window has all the characters of your team now if that is a case voila great man great um just one thing that firstly put your answer right now my answer is this window of size six size six again right now my answer was Infinity I have to minimize my window length so I put the answer as six but if your answer would have six just simply minimize the answer okay Infinity by default now it became a six now uh as we saw if the window a condition matches then try to shrink then try to shrink the window then try to shrink the window so what I will do is I I'll now when I like when I say Shing I'll just simply keep on moving my eye now when I say I'll keep on moving my eye I'll simply say that whatsoever eye you have let's say eye you have is here now simply move your eye ahead but as you're moving your eye you have to make sure one thing uh where is this gone as you move move your you have to make sure one thing that if I is going so which means I this character should come back to life so a is coming back to life now I simply increase the frequency of K A because it is coming back to life a is coming back to life so as I was moving onto character I was decreasing frequency so if something is coming back I'll increase the frequency now the same thing comes in picture if I if you increase the frequency if it becomes positive which means it is for sure it is for sure it is for sure it is for sure because of it was a line inside your string T and thus if something if the after after bringing back to life which means after increasing the frequency of that character a if it becomes positive it was because of it was in this inside my string T and thus please increase the count also increase the count also now the count becomes one which means which means now your window is not correct now window is not correct so simply again now keep on moving forward so now we have seen that as soon as we Tred to shrink again we know that we know the condition has matched so first I have to minimize my length so I just make sure I'm just minimizing my answer I just I'm just minimizing my answer now when that specific thing is done my minimization of answer and stuff is done so one thing is for sure that I will now increase try to increase my eye now when I say I will increase my eye I just simply keep on doing it while while while my window is valid and how is the valid condition window valid is only when the count is zero while my window is valid if my current window length if it is less than my minimum length current window length it is less than my minimum length then simply update the minimum length with the new minimum length and also if you make sure that RM y have taken minimum start because in the answer if you go back if I just show you in the answer you have to return um where is the gone in the answer you have just in the answer yeah in the answer you have to return a b n c which means a string to find the string you need to have the index of the string and the length of the string for to access the actual string which is the substring from the main string you have to know the index and the actual length so one thing we have seen so far is that we will actually make sure that we need both minimum start and the minimum length but but but if you had been following so far you must have seen a small catch Aran one thing while like you said that if you are at pointer J you will firstly check the pointer J and like as you mentioned before only you will decrease the frequency from s of J and then you will increase the pointer so and you said your J will always increase so your J would have already been increased and this was your I by default in the beginning so for sure for sure your window length will be J minus I only and Not Jus I + 1 so one thing is your window length currently is J minus I because your J has moved one step ahead already so that's your window length is J minus I and the same way same way here window L is J minus I because your J will for sure move ahead one step but but but as soon as it has moved now it will give a chance to I bro now you will try moving try moving okay the implementation as in the actual implementation will which means the utilization of the specific J movement will be seen in the next step next V Loop but it just says okay my J has moved next step and then again as as you can see also your J moves but as soon as the next while loop comes in picture its impact will be seen it's impact will be seen but right now the J has been moved also if you want what you can do is put the condition here itself and move your J in the very end of the code that is one way to do it then you can actually then you can actually use the same logic of J Min I +1 just make sure that pointer think that where you are placing your J I usually make sure I usually make sure thatth where is this gone I I I usually make sure that if I'm placing my piece of code of checking that window correct logic I usually if I place here then either my J is in the end or I place it after this specific condition if you placing after it which means your J has already moved ahead but the good practice is you can place it here also that's completely simple that's completely easy that's completely okay now as you can see um I'll simply move on move on my specific because of the window which I have moved on shring my I I actually minimize my window length and the same way goes that as you have shring the window which means the S of I tmap frequency will increase if the frequency increase becomes actually more than zero which means it is positive so simply increase the count also and also increase the ey pointer but this V Loop will only keep on going until unless your count is zero as soon as your count becomes more than zero sorry bro um you have got the incorrect window length thus you can simply keep on going forward now if you remember uh you will simply keep moving your J now as soon as your you moved your J okay your o frequency will decrease o frequency will become minus two then simply again the count is still a one uh your you will increase your J again the d d will come in picture D frequency will increase to minus 2 like decrease again uh then e will come in picture minus 2 then again B will come in picture B will come in picture okay it will become a one sorry it it will become a minus one because B is coming picture no if something is coming in picture it was a zero if something's coming in picture simply do a minus one as simple as that again the count will not decrease because the B by default frequency was not was was not positive then okay I'll simply keep on going on my a a was actually a one now I will actually decrease it my actually frequency will decrease it oh if it decreased which means for sure my count should also become zero now bro bro count becomes a zero if count become a zero then this is my specific window right so I'll just say my window will be Jus I + 1 by default again I'll show you both the codes how this Vari like VAR like Vari but if you increase your J right now then you will you can see that the window length is J minus I but yeah if you don't increase the window length right now like J right now so you can say okay in the next Val loop I increase my J now your window length is Jus I + 1 and thus and thus you can simply say that 1 2 3 4 5 6 7 8 9 10 but my by default length was six so it will not be minimized so length will still remain same but now I have opportunity to shrink my window so I'll just simply increase so D frequency will increase D frequency will increase become a minus one o frequency will increase become a minus one again move on I keep on moving ey B frequency will increase it will become a zero H again it has not become positive so you can never increase your count okay again um move your i e now it's a e e uh decrease the frequency increase the frequency become a one minus one again I C you have got a c ah simply you have got a C increase the frequency now it has become positive simply increase the account also because this is the new window which is valid after this as soon as you will move after as soon as you will move you are done you are done so here right now I have got a window length which is actually valid so 1 2 3 4 5 6 which is already same length so rather I should try minimizing the window length because it was a valid valid it's a valid window right it's a valid window so I just try to minimize the window length which means I'll go here as soon as I go here which means you saw that I tried to decrease the frequency of C it has become like frequency sorry it has I moved I has moved so for sure my frequency of C will increase count will also increase now kind my count has no longer remain zero so this is no longer a valid window length now if something is no longer valid window L simply keep on increasing your J okay n frequency will become minus one uh n frequency will become minus one now again move on J C frequency will become a zero count will become a zero this is a valid window length now okay I was here so uh increase the frequency of O it will become a zero then D it will become a zero then uh e it will become a zero then B it will become a it will become a one it has it has it now again it has become a one which means now okay now it is actually valid now it is actually valid again still it is so far so far it is valid I'll first try to minimize my length which is j- I + 1 so now my length will become a four again length will become a four now okay when I have minimized my length only then I will update as as as you have remembered first I'll try to minimize my length only then I will update my temp frequency and stuff so after I have checked off for my length minimize the length only then update it which means it will become a one now count will become a one so now it is no longer valid now simply keep on moving your J forward okay J forward and thus you can simply get the answer cool now uh it's exactly simple as simple as that the code here um I will just show you a quick modification in the code that rather than um again like first I'll show you exact implementation of everything which you have seen so far is as you saw that first we'll keep track of the T map T map is T T is the strin T and the frequency of that is the T map for us now I will have few variables I and J counter is the count which I have taken if you ever remembered by default the counter was the string length right I have those many number of characters for my string uh T minimum start and minimum length is for the final string final most optimal smallest window length I need to have a start for it and the length for it to actually find the substring for it and thus my n is nothing but I have to iterate on my J up till the nend so the simple Loop will while my J is less than n and I'll simply as I explained above also I'll simply try if my frequency of if if my frequency of j s of J is more than zero which means this character this s of J is inside my T string T So decrease the frequency decrease the count and also um decrease it everything this everything you will understand by the snippet of code which I have shown you above this this entire block I have showed you one at one place this entire block I have showed you at one place which means this is minimizing this is getting a minimum length and then as you move on your eye please please revert back all the operations which you have done now the only thing which I was saying is that right now it is J minus I because your J has already moved one step but but but but I also do like that if I put I if I put my J in the Super End which means right now your J will be at that specific Place only then what you can do is your current length will be j- I + 1 and the same way it will be J - I + 1 and that's the only change which you have to do and th if you try to submit it it will still work because it was exactly same now coming on back uh this was the portion where you move your J at every step as going to see J at every step because you have already moved your J so it's the reason I have put a j minus I but but but if you just put your J afterwards which means your J will move only in the end then you can just simply have a check minimize your length and then update move shrink your eye as you have shrinked your eye ultimately in the final answer you will have minimum start minimum start and minimum length as the same okay it is a starting length of the window having a minimum size and that is a minimum length for that window this will give me a subst strring if the minimum length is infinity so there is no substring simply an empty string and that's how you can simply Solve IT of n plus M time M time M time is to Simply make this specific hash map and N time is to iterate on this entire string of length n okay it's actually a 2 into n because your I and J J pointer will move one step I can also in wor case move one one step so it is a 2 into n 2 into n plus M and for sure space is just over 26 this is the most optimal solution which you can have see goodbye take care bye-bye again I specifically went on to these all five approaches to actually specify that what all you can tell in an
2024-03-20 11:41:02
76
https://i.ytimg.com/vi/X…axresdefault.jpg
76. Minimum Window Substring | 5 Improvizations | Sliding Window
cyIhOdKAoUE
all right let's talk about replace all digits with characters digits with characters so the idea is that you shift how many so the idea is that you shift how many times times after c so for for example fight after c so for for example fight five times after a you get f five times after a you get f and then there's a constraint it's and then there's a constraint it's guaranteed let your shift guaranteed let your shift never exist z so the maximum for a is 25 never exist z so the maximum for a is 25 a plus 25 is 26 right but for this is a plus 25 is 26 right but for this is simple simple a right a plus one is b and then a right a plus one is b and then c and d e and f right basically you are c and d e and f right basically you are taking the previous character plus taking the previous character plus one but the number could be different one but the number could be different like two like two three four right you just have to worry three four right you just have to worry about your tongue your constraint because for for this character you don't have number and then you end up just by itself then we need a stream builder to build now i can just do it like this now i can just do it like this make it easier and no matter what make it easier and no matter what right i have to append a pen see right i have to append a pen see okay so i have i have to check the okay so i have i have to check the character if the character character if the character is less than equal to 9 is less than equal to 9 or n sorry n c square equal to or n sorry n c square equal to 0 which which is which should be 0 which which is which should be one right okay one you can say zero but one right okay one you can say zero but zero doesn't work zero doesn't work the minimum number is one right the minimum number is one right should be one and should be one and we have to make sure i mean choosing we have to make sure i mean choosing case the previous character is quite a case the previous character is quite a good view good view right so index if i'm here right so index if i'm here i have to make sure this is this index i have to make sure this is this index is is valid so let's just say valid so let's just say new character neutral is equal to new character neutral is equal to uh stock chart at i minus one uh stock chart at i minus one but since this is the but since this is the taking the previous character plus the taking the previous character plus the number right number right and then the number will be c right it's the choice c right c minus zero right we don't want the zero technically so for the ascii value you subtract the subtract 48 and then this since this is char right we just have to cast a chart that will be your answer and you need you you need to append a pin neutral so when we append b right we don't want to append a again because c equal to a right so we continue right so id is pretty much it so i just have to return string for the string builder and check if i have arrow all right let's talk about what happened all right let's talk about what happened so there's a case for zero right so there's a case for zero right and then we have to worry about zero and then we have to worry about zero now checking again cool okay let's talk about timing space for the space is all the fun why because you are storing every single character into the string and for time it's all of them right you're looping every single character inside the screen and a little bit so time and space
2024-03-25 10:20:40
1,844
https://i.ytimg.com/vi/c…UE/sddefault.jpg
LeetCode 1844 | Replace All Digits with Characters | StringBuilder | Java
7HZmP8r1FBE
hi guys let's talk about a google interview question today the question is interview question today the question is lead code 147 or shuffle the array uh it lead code 147 or shuffle the array uh it goes something like this given that a goes something like this given that a number consisting of two n elements in number consisting of two n elements in the form x 1 x 2 x 3 until x n y 1 y 2 y the form x 1 x 2 x 3 until x n y 1 y 2 y 3 so on until y n you have to return the 3 so on until y n you have to return the array in the form x 1 y 1 x 2 y 2 x 3 y array in the form x 1 y 1 x 2 y 2 x 3 y 3 and so on until x and y n okay 3 and so on until x and y n okay so it's a lead code easy problem but it so it's a lead code easy problem but it becomes pretty hard to solve if someone becomes pretty hard to solve if someone asks you to solve it in one space and o asks you to solve it in one space and o in time complexity like it's easy to in time complexity like it's easy to solve in on space but one space is solve in on space but one space is pretty hard and in a google interview pretty hard and in a google interview you will be expected to solve it in o in you will be expected to solve it in o in space basically shuffle this array in space basically shuffle this array in place place so let's look at the input constraints so let's look at the input constraints input constraints are like n is less input constraints are like n is less than equal to 500 and than equal to 500 and length of basically n is the half of the length of basically n is the half of the size of the array because length of size of the array because length of array is 2n and num psi is greater than array is 2n and num psi is greater than equal to one less than equal to thousand equal to one less than equal to thousand okay so basically uh all the numbers in okay so basically uh all the numbers in the array are positive a pretty the array are positive a pretty important constraint which will help you important constraint which will help you develop a solution of open space okay develop a solution of open space okay and here's our test case as well like and here's our test case as well like nums is two five one three four seven n nums is two five one three four seven n is three which is half of the size of is three which is half of the size of array and output is two three five four array and output is two three five four one seven one seven anyways like uh you can solve it in o in anyways like uh you can solve it in o in space by creating a new array and just space by creating a new array and just copying the elements in the order copying the elements in the order specified in that new array very easy specified in that new array very easy but let's see how to do it in one space but let's see how to do it in one space so so like i have taken example here original like i have taken example here original is one two three four five six shuffled is one two three four five six shuffled array will be one four two five three array will be one four two five three six okay six okay and here i am denoting the mapping as and here i am denoting the mapping as well of the numbers from their old well of the numbers from their old position to the current position position to the current position uh basically sorry from the current uh basically sorry from the current position to the new position basically position to the new position basically the position in the original error and the position in the original error and the position the shuffled array okay the position the shuffled array okay like blue color is denoting all the like blue color is denoting all the numbers numbers uh in the first half of the array and uh in the first half of the array and red color is denoting all the numbers in red color is denoting all the numbers in the second half of their basically all the second half of their basically all the numbers whose index current index is the numbers whose index current index is greater than n and blue color current greater than n and blue color current index less than n okay index less than n okay so so this mapping actually helps you to this mapping actually helps you to visualize and uh visualize and uh like the position of the array in the like the position of the array in the original and position of the numbers in original and position of the numbers in the original and shuffled array and it the original and shuffled array and it also helps you determine a formula to also helps you determine a formula to come up with a formula for the uh for come up with a formula for the uh for the new positions of the elements okay the new positions of the elements okay and let's see what that formula is and let's see what that formula is so i have here in violet color i have so i have here in violet color i have written the indices of the written the indices of the like the current indices of the elements like the current indices of the elements in the original array and any n as i in the original array and any n as i said is the half of the size of array said is the half of the size of array so the new positions in the shuffled so the new positions in the shuffled array will be something like this like array will be something like this like if current position is less than a new if current position is less than a new position will be twice of current position will be twice of current position else new position equals twice position else new position equals twice of current position minus n plus one of current position minus n plus one okay and you can also see this formula okay and you can also see this formula is correct like uh for two the current is correct like uh for two the current position is one its new position will be position is one its new position will be uh twice of current position which is uh twice of current position which is two and you can see here in the blue two and you can see here in the blue color two is here which is at index two color two is here which is at index two and similarly for five like its current and similarly for five like its current position is four four is obviously position is four four is obviously greater than n which is 3 okay so its greater than n which is 3 okay so its new position will be twice of 4 minus 3 new position will be twice of 4 minus 3 that is 1 so 2 into 1 plus 1 that is 3 that is 1 so 2 into 1 plus 1 that is 3 and you can see here in the red color and you can see here in the red color that 5's new position is 3 and this that 5's new position is 3 and this formula also holds for the elements at formula also holds for the elements at the first and last position i mean their the first and last position i mean their position remains same in the original position remains same in the original and shuffled array like for one uh its and shuffled array like for one uh its new position will be twice of zero which new position will be twice of zero which is zero only and for six its new is zero only and for six its new position position will be twice of five position position will be twice of five minus three which is two into two 4 plus minus three which is two into two 4 plus 1 5 1 5 okay uh okay uh now what would be our algorithm now what would be our algorithm so algorithm would be something like so algorithm would be something like this that uh what we will try to do is this that uh what we will try to do is go in cycles in go in cycles in for each index like what we will do for each index like what we will do let's take an example for the number two let's take an example for the number two which is at index one which is at index one i determine the new position of two i determine the new position of two using this formula it comes out to be using this formula it comes out to be two only okay i will replace the number two only okay i will replace the number at the index two with at the index two with the number at position index one the number at position index one basically this two so what i would do basically this two so what i would do instead of placing two here i would instead of placing two here i would place minus two here okay and why i'm place minus two here okay and why i'm placing minus two is because i want to placing minus two is because i want to mark that i have actually visited this mark that i have actually visited this element element and and like since i cannot use an extra array like since i cannot use an extra array for marking that i have visited that for marking that i have visited that particular element i will particular element i will mark the visited as by keeping that mark the visited as by keeping that number as a negative number okay and number as a negative number okay and since i already told you in the since i already told you in the constraints we are given that all the constraints we are given that all the numbers are positive so that negative numbers are positive so that negative will not will not basically basically uh uh but you can say basically destroy our but you can say basically destroy our array okay array okay so so so what i would like i have replaced two so what i would like i have replaced two with this and then i would similarly with this and then i would similarly determine the position new position for determine the position new position for the number which was at index 2 and the number which was at index 2 and replaced replaced that number with this particular number that number with this particular number at index 2 and i will keep on doing that at index 2 and i will keep on doing that until i come to a position which was until i come to a position which was already visited okay in case you did not already visited okay in case you did not understand this uh let's actually try to understand this uh let's actually try to simulate this scenario okay so here i am simulate this scenario okay so here i am like my current number is two current like my current number is two current position is one uh which is here i position is one uh which is here i determine the new position which is n is determine the new position which is n is as i told you three new position will be as i told you three new position will be twice of uh twice of one which is two twice of uh twice of one which is two and the new number that is the number at and the new number that is the number at new position is three okay so what i new position is three okay so what i would do i would replace this number so would do i would replace this number so here i am i replace the number at index here i am i replace the number at index two with minus two okay and i then i two with minus two okay and i then i swap current and current number and swap current and current number and current position with new number and new current position with new number and new position so my current number has now position so my current number has now become three current position has become become three current position has become two i determine the new position for two i determine the new position for this which is twice of two which is four this which is twice of two which is four and at index four the number is five and at index four the number is five which is our new number so again i will which is our new number so again i will swap uh basically what i would do is swap uh basically what i would do is that i would replace the number at index that i would replace the number at index 4 with -3 4 with -3 so that's what i have done here i so that's what i have done here i replaced this with minus 3 now replaced this with minus 3 now and swapped new number and current and swapped new number and current number and new position current position number and new position current position current number is 5 current position is current number is 5 current position is 4 4 so new position will now become 3 so new position will now become 3 because like 4 is greater than 3 we'll because like 4 is greater than 3 we'll use this formula so it comes out to be 3 use this formula so it comes out to be 3 the number at index 3 is 4 which is our the number at index 3 is 4 which is our new number so now what i would do is i new number so now what i would do is i would replace the number at index 3 with would replace the number at index 3 with minus 5 so that's what i did i replace minus 5 so that's what i did i replace this number with minus 5 and swap new this number with minus 5 and swap new number current number new position number current number new position current position current position now our current number is 4 current now our current number is 4 current position is 3 we determine the new position is 3 we determine the new position again 3 is not less than n so position again 3 is not less than n so we did use this formula to determine new we did use this formula to determine new position which comes out to be 1 and position which comes out to be 1 and number at current sorry new position is number at current sorry new position is 2 2 so now i would replace the number at so now i would replace the number at index 1 with -4 so as you can see here index 1 with -4 so as you can see here that's what i did here i've denoted that's what i did here i've denoted these negative numbers with violet color these negative numbers with violet color and and as now our current number becomes two as now our current number becomes two current position becomes one i determine current position becomes one i determine the new position which comes out to be the new position which comes out to be two okay two okay so i would replace the number at index 2 so i would replace the number at index 2 with minus 2 so i did that with minus 2 so i did that okay and after doing that okay and after doing that what would happen is our current number what would happen is our current number will now become minus 2 and current will now become minus 2 and current position is 2 position is 2 so now since current number has become so now since current number has become minus 2 it means that we have already minus 2 it means that we have already visited this we do not need to you know visited this we do not need to you know go further in our cycle here we can stop go further in our cycle here we can stop this our cycle here this our cycle here and we will keep on doing that for each and we will keep on doing that for each position like this i have only done for position like this i have only done for one like if you remember here we started one like if you remember here we started with the with the number at position one but we will do number at position one but we will do this for each we will repeat do this this for each we will repeat do this cyclic thing for each position like for cyclic thing for each position like for two three four and five okay so this way two three four and five okay so this way you can clearly see that we have you can clearly see that we have basically basically not used any extra space and this will not used any extra space and this will also make sure that we you know also make sure that we you know transform our original error into the transform our original error into the shuffled array now the thing is shuffled array now the thing is like what is its time complexity so the like what is its time complexity so the time complexity is order n and let me time complexity is order n and let me tell you why is that so that is because tell you why is that so that is because this this visited thing like we are replacing visited thing like we are replacing every number with the negative of that every number with the negative of that number so that makes sure that every number so that makes sure that every index will be visited at most twice okay index will be visited at most twice okay once once uh like it would be visited once when we uh like it would be visited once when we encounter encounter basically when we are replacing that basically when we are replacing that number number that particular index and then there that particular index and then there might be a scenario that we might come might be a scenario that we might come back to that index again in our cycle back to that index again in our cycle but but at that moment we'll break out because at that moment we'll break out because you know uh we are using this negative you know uh we are using this negative as a visited flag as a visited flag so this thing make sure that all the so this thing make sure that all the indices are visited at most twice only indices are visited at most twice only so we will at most do like two end so we will at most do like two end traversals traversals and and sorry to win iterations sorry to win iterations so so uh uh and and let me show you one more thing like this let me show you one more thing like this we have done only for the index one then we have done only for the index one then we go to index two so at index two we go to index two so at index two we see that the number is already we see that the number is already negative so we do not do anything here negative so we do not do anything here current number is already negative we go current number is already negative we go to index three again current number is to index three again current number is already negative minus five we go to an already negative minus five we go to an index four current number already index four current number already negative minus three then at five negative minus three then at five like uh last this is the last number so like uh last this is the last number so last number will remain last number will remain at its own position and we finally break at its own position and we finally break out of this loop so out of this loop so you can clearly see that this visited you can clearly see that this visited flag took care of not repeating our flag took care of not repeating our iterations iterations and you can also see that this uh like and you can also see that this uh like after doing this what we will do is that after doing this what we will do is that we iterate over the array once more we we iterate over the array once more we will replace all the numbers with will replace all the numbers with negative numbers with positive negative numbers with positive value of that particular number and uh value of that particular number and uh finally we will get our original finally we will get our original shuffled array basically our expected shuffled array basically our expected shuffled array which is one four two shuffled array which is one four two five three six and if you remember this five three six and if you remember this was the answer as well one four two five was the answer as well one four two five three six so let's look at the code for three six so let's look at the code for it it uh so here is the code which i had uh so here is the code which i had written like this is the api we had to written like this is the api we had to implement and i implement and i what i am going to do is that for each what i am going to do is that for each index i will try to index i will try to you know do that cyclic process you know do that cyclic process so that's why i'm iterating here from i so that's why i'm iterating here from i equal to 0 to length of the array and equal to 0 to length of the array and initialize current number as num psi initialize current number as num psi current position is i and we only current position is i and we only basically basically do you know iterations in this while do you know iterations in this while loop until this current number is loop until this current number is positive until this current number is positive until this current number is greater than zero and greater than zero and we figure out the new position based on we figure out the new position based on current position and this is that current position and this is that particular formula and then what we do particular formula and then what we do is that we would replace the number at is that we would replace the number at new position with the negative of the new position with the negative of the current number and we will then swap the current number and we will then swap the current number and current position with current number and current position with the new number and new position okay and the new number and new position okay and we'll keep on doing that we'll keep on doing that and eventually when uh after this for and eventually when uh after this for loop we iterate over the array again and loop we iterate over the array again and if we see that num size less than zero if we see that num size less than zero we replace it with the positive value of we replace it with the positive value of that uh number and eventually we return that uh number and eventually we return the num array so time complexity o n the num array so time complexity o n space complexity oven we have solved a space complexity oven we have solved a google hard interview question good to google hard interview question good to move to second round thank you guys for move to second round thank you guys for watching please do not forget to like watching please do not forget to like subscribe and comment on this video i'll subscribe and comment on this video i'll see you all next time
2024-03-21 02:57:10
1,470
https://i.ytimg.com/vi/7…axresdefault.jpg
Leetcode Shuffle the Array | O(1) space
FKmt7UvCpMI
hey everyone and welcome to another Elite code problem so today we're going Elite code problem so today we're going to be doing problem number 489 robot to be doing problem number 489 robot room cleaner room cleaner and it's a backtracking problem it's a and it's a backtracking problem it's a hard one it's probably hard one it's probably one of the hardest backtracking problems one of the hardest backtracking problems I've done I've done um even if you know like backtracking um even if you know like backtracking there's a lot of annoying parts of this there's a lot of annoying parts of this problem for sure so I would encourage problem for sure so I would encourage you if you are learning backtracking it you if you are learning backtracking it is on the backtracking explore part I is on the backtracking explore part I would encourage you to try this one for would encourage you to try this one for sure sure um just to test your you know um just to test your you know understanding of backtracking and the understanding of backtracking and the hard thing about this problem is I'll hard thing about this problem is I'll just tell you is so you have this robot just tell you is so you have this robot in a grid and you don't know you're not in a grid and you don't know you're not given access to the grid the only thing given access to the grid the only thing you're given access to is a couple you're given access to is a couple functions for the robot functions for the robot so you're given a move function that so you're given a move function that will return true if the robot can make a will return true if the robot can make a valid move and then you can turn the valid move and then you can turn the robot left and right and it starts off robot left and right and it starts off facing up facing up and so yeah the annoying thing is like and so yeah the annoying thing is like you don't you don't see the grid you you don't you don't see the grid you don't really know where the robot is don't really know where the robot is and it's hard to visualize if what your and it's hard to visualize if what your algorithm is doing is correct also the algorithm is doing is correct also the other annoying thing about this other annoying thing about this backtracking um is like let's say you're backtracking um is like let's say you're in some cell in some cell draw a grid here draw a grid here so let's just draw a simple grid so let's just draw a simple grid three by three grid with no obstacles and let's say here's our robot so Circle it's going to be facing up so The Annoying Thing is how to figure out well how do I actually backtrack like let's say I call move and the robot moves up here it's still so whenever you move you're still facing the same direction but how do I actually backtrack like like what's my backtracking algorithm like if I return it's still going to be in the wrong grid so what so how do I and so what you're going to want to do and so what you're going to want to do is you can visualize I mean when you go is you can visualize I mean when you go somewhere your end State needs to be somewhere your end State needs to be your robot needs to turn around right so your robot needs to turn around right so it needs to face this way it needs to face this way and then you need to call move and then you can handle backtrack in one of two ways so you can either move back and then return and then like turn it around in the the function right before you return or you could handle it in the actual backtrack function so let's say you just call backtrack right you call backtrack with some you know you move and then you call backtrack then what you can actually do is once the function before returns like let's say this function you can call backtrack the robot's over here and then it's it's it's looking the way you want it to let's say for the ending state maybe you want it to look like this I don't know there's a bunch of ways to do it then what you would have to do is well what would you have to do to make this state turn into this state right and this is our original state and to fully complete our backtracking so in this case we'd have to turn left or right twice so we'll just say turn left twice so we can just call that function then we have to go move and then we can turn so hopefully that makes sense when you so hopefully that makes sense when you have this robot you turn it around have this robot you turn it around so a 180 degree rotation would be left so a 180 degree rotation would be left or right twice then you'd call move then or right twice then you'd call move then it would go here and then you'd turn it would go here and then you'd turn around again and then that would give around again and then that would give you the original state of the robot you the original state of the robot before it entered this so this is this before it entered this so this is this is how you would get out is how you would get out now the other annoying thing for so for now the other annoying thing for so for most backtracking problems well it most backtracking problems well it actually depends but actually depends but let's say you do have some backtracking let's say you do have some backtracking algorithm and let's say you're algorithm and let's say you're backtracking algorithm just backtracking algorithm just so let's say you try the cell in front so let's say you try the cell in front of you of you and then if it doesn't work you just and then if it doesn't work you just keep turning right and you keep turning keep turning right and you keep turning right and you keep turning right well right and you keep turning right well let's see what would happen let's see what would happen so we we can't go here so we're gonna so we we can't go here so we're gonna turn right turn right okay and we're remember when we go into okay and we're remember when we go into a cell we Face the same direction that a cell we Face the same direction that we came in we can't go here we turn we came in we can't go here we turn right right it's going down it's going down okay we can go here okay we can't go okay we can go here okay we can't go down here so we turn right down here so we turn right okay can't go down here okay well now we can't go here we go okay well now we can't go here we go here here so how do you how do we even know and so how do you how do we even know and then we end up here how do we even know then we end up here how do we even know we ended like we don't have we don't we ended like we don't have we don't have a grid remember and in this case it have a grid remember and in this case it worked out but in other cases uh you can worked out but in other cases uh you can try out some other examples but some try out some other examples but some other examples when you do this when you other examples when you do this when you just keep turning right just keep turning right here so in this case you did end up here so in this case you did end up where you started but in a lot of cases where you started but in a lot of cases you'll just run into like a loop you'll just run into like a loop and how do you even know and then you and how do you even know and then you just you just hit maximum recursion just you just hit maximum recursion depth right because you keep looping but depth right because you keep looping but you don't even know that you cleaned you don't even know that you cleaned every room and you can't check that you every room and you can't check that you cleaned every room you don't know how cleaned every room you don't know how big the grid is you're not giving access big the grid is you're not giving access to any of that information all you can to any of that information all you can do is move the robot do is move the robot and so how would we solve that problem and so how would we solve that problem and to solve that problem this is also and to solve that problem this is also kind of really annoying actually what kind of really annoying actually what we're going to do is we're gonna have a we're going to do is we're gonna have a visited set and you may ask well how do visited set and you may ask well how do you have a visited set if you don't even you have a visited set if you don't even have an index of like where you're have an index of like where you're starting starting and so what we actually need to do that and so what we actually need to do that makes this problem difficult is you have makes this problem difficult is you have a visited set a visited set and then you need to figure out what and then you need to figure out what index what at least what relative index index what at least what relative index you're at and so let me show you what you're at and so let me show you what that looks like that looks like so let's say we let's call this index 0 so let's say we let's call this index 0 0 right so we just say inertial index is 0 right so we just say inertial index is zero zero so we put zero zero in here zero zero so we put zero zero in here and now by the way when we Traverse and now by the way when we Traverse we're gonna only Traverse if our we're gonna only Traverse if our if the place we're traversing isn't in if the place we're traversing isn't in our visited set because we don't need to our visited set because we don't need to visit the same room twice also for this visit the same room twice also for this backtracking that makes it different backtracking that makes it different from some other ones like a Sudoku is from some other ones like a Sudoku is even if you like once you're on a path even if you like once you're on a path and you go to a bunch of rooms when you and you go to a bunch of rooms when you backtrack to where you started you never backtrack to where you started you never have to go back to any of those rooms have to go back to any of those rooms again because we of we've already again because we of we've already cleaned them and we've already went as cleaned them and we've already went as far as we could from any of those rooms far as we could from any of those rooms so once we have visited set and we add so once we have visited set and we add items we're never going to remove them items we're never going to remove them in a normal backtracking you normally do in a normal backtracking you normally do remove and you kind of check for a remove and you kind of check for a Final Solution type of thing you know Final Solution type of thing you know like like for end Queens you would go like like for end Queens you would go down all the rows and you have to see is down all the rows and you have to see is there a final solution and you can you there a final solution and you can you keep removing Queens from the set but in keep removing Queens from the set but in this case once you were in a room once this case once you were in a room once you never have to clean it again because you never have to clean it again because as soon as you go into room we can just as soon as you go into room we can just hit clean and then it's clean hit clean and then it's clean so let's say we're here so what we're so let's say we're here so what we're actually going to use is we're going to actually going to use is we're going to use use um to actually use this for a lot of DFS um to actually use this for a lot of DFS so we're actually going to have an array so we're actually going to have an array like this of directions and we're gonna like this of directions and we're gonna so normally when I have this array of so normally when I have this array of rays for different directions I have rays for different directions I have like one zero zero one negative one zero like one zero zero one negative one zero and so on but here the mistake we need and so on but here the mistake we need to not make is to not make is we need to make sure that so we're going we need to make sure that so we're going to be turning right a lot to be turning right a lot and now we need to figure out what are and now we need to figure out what are the values that you need to go up the values that you need to go up and then if you turn right we're gonna and then if you turn right we're gonna try to make sure that the value to the try to make sure that the value to the right of the current one we're at is the right of the current one we're at is the right thing so to go up actually it right thing so to go up actually it would be this would be this we are going up a row we are going up a row now if we're going up now if we're going up and we turn right now what is okay now and we turn right now what is okay now what is this this is column plus one what is this this is column plus one and so this is another tricky thing is and so this is another tricky thing is you have to make sure you arrange these you have to make sure you arrange these in the right order and for this problem in the right order and for this problem so this is going to be 0 1 so this is going to be 0 1 okay now okay now down if we go right so down is one zero down if we go right so down is one zero and then so yeah if you do if you do a and then so yeah if you do if you do a normal like stuff they use for other normal like stuff they use for other functions you're going to get an error functions you're going to get an error so now we were down now we need to go so now we were down now we need to go right or sorry left right so down if we right or sorry left right so down if we turn right from down it's a left turn right from down it's a left and left is actually and left is actually 0 minus one 0 minus one and so this is what it's going to look and so this is what it's going to look like and all we're going to do when we like and all we're going to do when we turn right is we're just going to say turn right is we're just going to say let's just go to the next Direction let's just go to the next Direction let's go to the next Direction and when let's go to the next Direction and when we reach the last one we can simply just we reach the last one we can simply just go back to the first one so let's see what kind of that looks like so we're at zero zero now we know we're going up so if we're going up that means our row is minus one right so then that would be I might cut out for a second so this I might cut out for a second so this index is going to be negative one zero index is going to be negative one zero now we tried to go up we can't we go now we tried to go up we can't we go right so then what is right going to be right so then what is right going to be well right it's going to be negative 1 1 well right it's going to be negative 1 1 and so on so you're gonna you're gonna and so on so you're gonna you're gonna have these relative indices instead of have these relative indices instead of absolute indices so even though you absolute indices so even though you don't know how big the grid is you have don't know how big the grid is you have to know which way you're facing and to know which way you're facing and where you started and from there you can where you started and from there you can figure out what relative index you're at figure out what relative index you're at and so we're going to keep adding to our and so we're going to keep adding to our visited set until we can't go anymore visited set until we can't go anymore and then we are going to like I said and then we are going to like I said when we're facing some Direction so it's when we're facing some Direction so it's going to look like like this is what a going to look like like this is what a backtracking would look like for just backtracking would look like for just one particular square right so if we one particular square right so if we have a square have a square let's say we came from here let's say we came from here so we came from here we're in the Square so we came from here we're in the Square let's say we can't go anywhere so we try let's say we can't go anywhere so we try to go here can't we turn right to go here can't we turn right try to go here can't try to go here can't turn right try to go here can't and because we can't because it's visited then we turn right again and then we try to go here and we can't and then we turn right I'm gonna double check the code but our ending State wants to be like this so in our after we tried everywhere we want to be here yeah so actually first we don't turn right so we turn right four times total so we try here we can't try here can't try here can't try here can't and then we turn right one last time until we turn right four times total and if we turn right right we make 360 degree and then once we're actually going down here then we simply to go back like I said we turn right twice so then we turn around and we go this way we hit move so then we're back in this cell and then we simply turn right two more times either and and you can do this either in either method as long as you do it because even once you're back in the original method you can actually hit move and if it's like an invalid location as soon as you've act as um as long as you've cleaned all the cells it doesn't really matter like if you try to move and you and you recurse back to the original method it'll be fine okay so let's take a look at the code and we'll explain that so I actually didn't want to code this up on the Fly because there are definitely I would there's yeah there's not a lot of code but there's a ton of Errors you can make here okay and so like I said they said these are the directions so this is representative of up then we have right and we have down then we have left oops and we have a visited set and then we have this backtrack function so first thing we do when we get into a room is we clean the room right so as soon as it doesn't hurt us to clean the room so we might as well do that then we add that room to our visited set and then here's our Loop where we try every single Direction and so to get a new Direction our our initial direction is going to be up which is going to be this this thing right here and so to get a new direction we're just going to have an i and a range four and then we are going to do Direction plus I mod 4. and what that does is it starts with zero right so the original direction would be this then it'll add this then a lot of this but let's say our Direction our original direction is three maybe so we're on this one then if we add one and mod it by four it'll actually loop back to the beginning of the list so let's make sure we're not out of bounds and then we just loop back to the beginning of the list then recalculate a new row and a new column based on the direction we're at and the current row and column we're in and then remember so this is also another thing you can screw up when you do this check to make sure that your row and column isn't in your visited set and this robot move make sure that you check that the row and column isn't in the visited set before you do this robot move because if you do the robot move now you're going to be in another square and then you're not gonna like this stuff isn't gonna line up so you have to make sure it's a valid place to go before you even try to go there otherwise yeah like even if you can't if you can't go there it'll be fine but if you can go there and it is in your business that's going to screw your code okay so now we backtrack to the new uh Direction and new row column then remember when we backtrack this is when we're recursed out so in my code I made it do this 180 right right after you right after you leave the back trap Loop you can do this in a bunch of different ways and then we turn right so we turn right four times as I said so really what's going to happen is this is going to be when we when we return from a square right so let's say we say we were in this top square we went down right then we returned from a square well the way my code works is when we're returned we're actually going to return facing the other way and so when we return we just need to 180 around to be in the original state we were in before we backtracked and so that's what that does so turn right we check remember we check the next place we can go to and then this is simply what we do before we backtrack out so when we and like I said I could have done I'm pretty sure if I wrote this code uh in here this would have worked too I don't want to test it actually I can do it I can try to test it so let's just see let's say we backtrack out and we literally do it right here instead what happens it might work maybe there's some other thing that I need to think of okay so it actually does work so that's okay so it actually does work so that's another way you can backtrack out is so another way you can backtrack out is so now what I did instead of my original now what I did instead of my original thing is I have something like this thing is I have something like this and so let's say I'm here and so let's say I'm here and now I would be actually and now I would be actually see here so I moved I turned around see here so I moved I turned around twice and then I move yeah okay so so I twice and then I move yeah okay so so I believe that the final state is like believe that the final state is like this of the cell that I backtrack to so this of the cell that I backtrack to so what I need to do is I need to turn it what I need to do is I need to turn it around right then I need to move back around right then I need to move back and then I need to turn it around again and then I need to turn it around again and that would make it the same state and that would make it the same state here here so like I said you can put this code in so like I said you can put this code in multiple different places but basically multiple different places but basically yeah you have to make sure you handle yeah you have to make sure you handle this and turn right to Nursery you know this and turn right to Nursery you know you want to turn right as many times as you want to turn right as many times as you turn left so the the gears on the you turn left so the the gears on the robot you know don't get rusty you don't robot you know don't get rusty you don't want to keep turning right only okay so want to keep turning right only okay so anyway so yeah so you go back and then anyway so yeah so you go back and then this is the initial function call and I this is the initial function call and I just made it pass with one one zero just made it pass with one one zero because and so what I would encourage because and so what I would encourage you to do by the way if you're getting a you to do by the way if you're getting a lot of Errors is lot of Errors is maybe maybe um one thing that helped me is this this um one thing that helped me is this this grid is really big so you can just make grid is really big so you can just make a custom grid and you can you can even a custom grid and you can you can even try something like this you know where try something like this you know where you have a custom grid and this is the you have a custom grid and this is the custom grid I used where every cell custom grid I used where every cell doesn't have anything and then I just doesn't have anything and then I just made all of these ones one meaning it made all of these ones one meaning it doesn't have a uh it doesn't have brick doesn't have a uh it doesn't have brick so it needs to be cleaned and then I so it needs to be cleaned and then I made my starting location this and then made my starting location this and then I would try to draw out the states of I would try to draw out the states of the robot on my grid and then also print the robot on my grid and then also print like the visited set and where it was in like the visited set and where it was in and make sure it matched because that's and make sure it matched because that's how you catch a lot of your areas that's how you catch a lot of your areas that's what saved me a lot of time for sure what saved me a lot of time for sure um so and then you can also do that by um so and then you can also do that by if you don't know in this new lead code if you don't know in this new lead code version what you can actually do is you version what you can actually do is you can go to test case you can add a test can go to test case you can add a test case case and so you can say like if you know what and so you can say like if you know what it looks like so something like this it looks like so something like this you can add that in you can add that in copy that so this would be a three by copy that so this would be a three by three array three array if you wanted to and then you could say if you wanted to and then you could say yeah like start at you know one one and yeah like start at you know one one and maybe print maybe print my visited Saturn row column or whatever my visited Saturn row column or whatever it is when you have errors because like it is when you have errors because like if you think about it there's there's no if you think about it there's there's no really way to test for this what makes really way to test for this what makes it really annoying and difficult is it really annoying and difficult is there's no really way to test your code there's no really way to test your code like when you have a bug it'll just say like when you have a bug it'll just say all the rooms weren't cleaned and you all the rooms weren't cleaned and you don't really know where the robot was or don't really know where the robot was or where it went because you have no access where it went because you have no access to the grid so it's a real pain to try to the grid so it's a real pain to try to debug it unless you actually draw at to debug it unless you actually draw at the grid yourself and try to follow like the grid yourself and try to follow like is the robot facing the right way is it is the robot facing the right way is it going in the right order by checking going in the right order by checking where the visited set is and where the where the visited set is and where the column is and so on and so I do think column is and so on and so I do think this is a great problem for backtracking this is a great problem for backtracking it definitely does get annoying with you it definitely does get annoying with you have to do a lot of israelization and have to do a lot of israelization and figure out what things have to look like figure out what things have to look like with limited information so with limited information so okay so let's do the time in space okay so let's do the time in space complexity and actually the time and complexity and actually the time and space complexity for this prom are space complexity for this prom are pretty good compared to a normal pretty good compared to a normal backtracking algorithm normally they're backtracking algorithm normally they're very inefficient and so here because very inefficient and so here because we're never actually removing from the we're never actually removing from the visited set we're not visiting a set visited set we're not visiting a set we're not visiting a cell more than we're not visiting a cell more than twice twice so we are only visiting cells that so we are only visiting cells that are ones and not uh so let's say zero is are ones and not uh so let's say zero is a brick we're only visiting cells that a brick we're only visiting cells that are one so it'd be something like count are one so it'd be something like count ones mine or just be count ones right so ones mine or just be count ones right so if we have whatever many empty cells we if we have whatever many empty cells we only visit them one time only visit them one time we do have to technically go back we do have to technically go back actually but actually but I think that is not going to add I think that is not going to add complexity so that's fine so what did we do for space so we do so what did we do for space so we do have a have a we do have we do have um um this visited set right this visited set right and so this visited set is going to have and so this visited set is going to have every single every single so this is kind of the same thing it's so this is kind of the same thing it's going to have every single room we going to have every single room we visited but we can only visit rooms with visited but we can only visit rooms with cat ones cat ones so that's just going to be big of count so that's just going to be big of count ones ones and yeah like I said if we clean a room and yeah like I said if we clean a room and we backtrack and we backtrack uh I think that should be constant time uh I think that should be constant time to backtrack back to backtrack back or or yeah it's actually going to be no it's yeah it's actually going to be no it's actually gonna be like Big O of so we so actually gonna be like Big O of so we so we do actually have to visit a room and we do actually have to visit a room and we and we backtrack back so let's say we we and we backtrack back so let's say we we go something like this we go something like this like this is our path and we have to like this is our path and we have to backtrack back how much does that cost backtrack back how much does that cost like like our maximum call stack does get to like our maximum call stack does get to like the number of rooms we visited but we're the number of rooms we visited but we're basically just visiting a room and then basically just visiting a room and then coming back so it's going to be like Big coming back so it's going to be like Big O of 2N instead of n it's not going to O of 2N instead of n it's not going to add any more and then once we did add any more and then once we did visited these rooms and come back we can visited these rooms and come back we can never go into any of any of these rooms never go into any of any of these rooms that we visited ever again because we're that we visited ever again because we're never getting Rhythm and they've visited never getting Rhythm and they've visited um in the visited set that that's what um in the visited set that that's what would make things more expensive and would make things more expensive and that's why most backtracking operations that's why most backtracking operations are more expensive because you have to are more expensive because you have to you have to fill in let's say nine you have to fill in let's say nine different you know problems and then you different you know problems and then you can have repeated work where you're here can have repeated work where you're here you can't once you've visited a room you can't once you've visited a room you're done you're done okay so that's all for this problem and okay so that's all for this problem and if you like the solution app please like if you like the solution app please like And subscribe to the video it helps grow And subscribe to the video it helps grow this Channel and I'll be posting more in this Channel and I'll be posting more in the future so I'll see you in the next the future so I'll see you in the next one
2024-03-22 15:38:25
489
https://i.ytimg.com/vi/F…MI/sddefault.jpg
Robot Room Cleaner
Hz4Fv0pa1vM
cool 62 unique paths will bite it's located on the top left corner over located on the top left corner over m-by-n great the robot can only move m-by-n great the robot can only move either down or right at any point in either down or right at any point in time well by trying to reach the bottom time well by trying to reach the bottom right corner and the grid how many right corner and the grid how many possible unique paths are there ok it's possible unique paths are there ok it's a little so actually I mean this is a little so actually I mean this is actually a very straight forward I want actually a very straight forward I want to say dynamic pole and well and it's to say dynamic pole and well and it's not straight for in a sense that is you not straight for in a sense that is you know you've come up with it's just very know you've come up with it's just very standard in the sense that it's a very standard in the sense that it's a very possible textbook type album yeah like possible textbook type album yeah like yeah yeah my only concern and that's I'm missing my only concern and that's I'm missing something it's kind of like what happens something it's kind of like what happens if the number gets too big because if the number gets too big because easily with a man and it's a hundred yeah yeah and that's I'm missing yeah yeah and that's I'm missing something like you could definitely over something like you could definitely over for long or and a lot very quickly and for long or and a lot very quickly and actually there's a halving the thing to actually there's a halving the thing to notice here is that it's actually I mean notice here is that it's actually I mean once again this is a straightforward once again this is a straightforward issue unquote dynamic programming issue unquote dynamic programming problem which stems from I mean it's problem which stems from I mean it's very textbook me in a sense once you very textbook me in a sense once you kind of get it down but the other fun kind of get it down but the other fun thing about this poem is that it's thing about this poem is that it's actually related to how you calculate actually related to how you calculate binomials in a sense that just where I binomials in a sense that just where I wish I could draw but you know binomial wish I could draw but you know binomial type things and I could we'll try to type things and I could we'll try to attempt some ASCII or attempt some ASCII or so bear with me for a second but let's so bear with me for a second but let's say you have a tough thing say you have a tough thing oh yeah so the binomial is X and so you oh yeah so the binomial is X and so you have like the top layer and then you have like the top layer and then you have to was a car like 1 plus X 2 man I have to was a car like 1 plus X 2 man I I don't remember my mathematical I don't remember my mathematical formulations to be honest but uh but formulations to be honest but uh but yeah but on the next level yeah but on the next level it's huh it's 1 plus X and then I know it's huh it's 1 plus X and then I know the next level is 1 plus 2x plus x the next level is 1 plus 2x plus x squared oops and and so forth I mean you squared oops and and so forth I mean you know you know their expansion for that know you know their expansion for that kind of worries for me love it and you kind of worries for me love it and you get this so that's kind of like I mean get this so that's kind of like I mean you could keep on drawing this this is you could keep on drawing this this is where is like you learn it in greats where is like you learn it in greats not one of the great schools or maybe not one of the great schools or maybe high school I don't remember anymore and high school I don't remember anymore and that's just like you know and actually that's just like you know and actually tip yeah that's why I messed up so one tip yeah that's why I messed up so one in time and then just and then this in time and then just and then this obviously this corresponds to 1 plus X 2 obviously this corresponds to 1 plus X 2 is 0 this is 1 plus X to the first and is 0 this is 1 plus X to the first and then this is 1 plus X to the second and then this is 1 plus X to the second and so forth right so then that's the kind so forth right so then that's the kind of the pattern here and it it turns out of the pattern here and it it turns out that this thing also is a way to kind of that this thing also is a way to kind of like it's I think they're called like it's I think they're called something like generating functions something like generating functions attire there's like a decade of math ago attire there's like a decade of math ago hey back to a follow him strike him trip hey back to a follow him strike him trip em but yeah but that's the general thing em but yeah but that's the general thing here and that actually and you could and here and that actually and you could and kind of a standard way to explain it is kind of a standard way to explain it is that and you could I forget what that that and you could I forget what that parameters core I just finger was PI parameters core I just finger was PI Gnomeo bread Gnomeo bread scouts tryingo that's what I'm thinking scouts tryingo that's what I'm thinking of way and this is a Pascal triangle of way and this is a Pascal triangle thing where you know you could think thing where you know you could think about it as 1 and then 1 and then 1 2 1 about it as 1 and then 1 and then 1 2 1 go say the same pattern you Center what go say the same pattern you Center what I did before and and where this node is I did before and and where this node is just the sum of the two two entries and just the sum of the two two entries and you know in the world before and so you know in the world before and so forth way dot dot dot forth way dot dot dot so this is Pascal's triangle which so this is Pascal's triangle which relates to this other thing that I guess relates to this other thing that I guess I should have started with the Pascal I should have started with the Pascal triangle by just blank underneath but triangle by just blank underneath but anyway so the fun thing about this is anyway so the fun thing about this is that this is if you kind of tilt the that this is if you kind of tilt the board a little bit or like you look at board a little bit or like you look at it in a diagonal way now now you could it in a diagonal way now now you could see your entries are actually like 1 1 see your entries are actually like 1 1 on the first world 1 in the first column on the first world 1 in the first column and then 2 3 and so forth and then you and then 2 3 and so forth and then you told your head a little bit you could told your head a little bit you could get like the diagnosis so this this maps get like the diagnosis so this this maps to these in the diagonals right I don't to these in the diagonals right I don't even know how George is on ASCII or even know how George is on ASCII or despair but like diagonal for the first despair but like diagonal for the first row and or first first diagonal and so row and or first first diagonal and so forth so those are your things right and forth so those are your things right and also it the fun thing is that this is also it the fun thing is that this is also the number of unique paths because also the number of unique paths because you could look at you should know to be you could look at you should know to be like okay how many ways do I get here like okay how many ways do I get here well just one way to get to this cell well just one way to get to this cell and there's one way to get to this cell and there's one way to get to this cell so there's two ways to get here and then so there's two ways to get here and then how do I get to here how do I get to here well there's two ways to get to this well there's two ways to get to this cell and there's one way to get to this cell and there's one way to get to this cell so that did doing ways to get to cell so that did doing ways to get to this cell and so forth this cell and so forth and anyway so there's all these math and anyway so there's all these math things so anyway there's a very long things so anyway there's a very long explanation hopefully it's kind of explanation hopefully it's kind of interesting to you but it's a long way interesting to you but it's a long way of saying that's how you would solve of saying that's how you would solve this problem in the same way and if we this problem in the same way and if we left a binomial and Pascal's triangle left a binomial and Pascal's triangle and all these other fun stuff but one and all these other fun stuff but one but one more thing that I will leave you but one more thing that I will leave you with with this problem then with with this problem then is actually another kind of interesting is actually another kind of interesting thing is if we sum up the rows of in thing is if we sum up the rows of in Pascal's triangle which is also Pascal's triangle which is also diagonals so this is actually if you sum diagonals so this is actually if you sum it up is two to the zero and if you sum it up is two to the zero and if you sum this up is two to the one and you could this up is two to the one and you could see that easy and then here is two to see that easy and then here is two to the two and two to the third and so the two and two to the third and so forth right and you could kind of make forth right and you could kind of make this proof the other way very quickly this proof the other way very quickly because for each entry in the previous because for each entry in the previous world world you're gonna for you to of those paths you're gonna for you to of those paths there'll be two paths outweigh so you there'll be two paths outweigh so you always double the paths every time where always double the paths every time where so I said so on each row the sum is 2 to so I said so on each row the sum is 2 to the K or whatever so that's kind of one the K or whatever so that's kind of one interesting thing about it and that also interesting thing about it and that also goes back to there's another way to goes back to there's another way to prove this but another way to kind of prove this but another way to kind of visualize it with binomials because visualize it with binomials because we're binomial is another way to think we're binomial is another way to think about it it's like this entry is also about it it's like this entry is also the number of ways to choose one given the number of ways to choose one given three elements choose one element way three elements choose one element way this is given three elements true zero this is given three elements true zero do we animal choose to do a true story do we animal choose to do a true story right right and something so like so that's yeah so and something so like so that's yeah so it's 3 to 0 3 choose 1 3 and then you it's 3 to 0 3 choose 1 3 and then you see somewhere that should be 2 2 2 to see somewhere that should be 2 2 2 to the K because that's literally the the K because that's literally the number of possibilities you can have number of possibilities you can have because that's just you know like the in because that's just you know like the in our counting type thing way anyway so our counting type thing way anyway so that's a very long-winded way of saying that's a very long-winded way of saying but the reason why I kind of brought but the reason why I kind of brought this up and not just to flex or show off this up and not just to flex or show off or whatever is that it's something that or whatever is that it's something that I think about when I see this form I think about when I see this form because when m and n will be at most 100 because when m and n will be at most 100 what that means is that so like you know what that means is that so like you know just let's say this is 100 I mean I skip just let's say this is 100 I mean I skip ahead a little bit so I gave this entry ahead a little bit so I gave this entry here so yeah I am and I forget I guess here so yeah I am and I forget I guess we should check with you and then like we should check with you and then like in the corner here in the corner here is M plus n right and and the thing here is M plus n right and and the thing here is that as we talked about before so is that as we talked about before so this is a most going to be M Jews oh this is a most going to be M Jews oh sorry sorry M this is M plus and you know what is M this is M plus and you know what is that well I guess I've always MPUs well that well I guess I've always MPUs well that diagonal is ampersand and choose that diagonal is ampersand and choose what know the sum of that one is oh I what know the sum of that one is oh I don't remember don't remember man I'm I'm mixing myself up it's been a man I'm I'm mixing myself up it's been a while but but the point is that this is while but but the point is that this is dominated by like the sum of that dominated by like the sum of that diagonal you will oh yeah oh wait no cuz diagonal you will oh yeah oh wait no cuz okay I'm thinking of this tag so I I was okay I'm thinking of this tag so I I was doing the math with this place where has doing the math with this place where has to be between m and n there's some eggs to be between m and n there's some eggs but if you're doing like diagonal then but if you're doing like diagonal then then M plus N is under on this diagonal then M plus N is under on this diagonal which is yeah so it's M plus and choose which is yeah so it's M plus and choose notice M times n times choose M percent notice M times n times choose M percent I don't know but the point is that this I don't know but the point is that this has a bound of like something like 2 to has a bound of like something like 2 to the M plus N and that was what concerned the M plus N and that was what concerned me when I saw that M plus n will be 100 me when I saw that M plus n will be 100 because you got a really big number is because you got a really big number is my point so and looking up and down my my point so and looking up and down my other daughter about it is to see if other daughter about it is to see if they have any kind of strange to be like they have any kind of strange to be like oh hey but it's ok because you know it's oh hey but it's ok because you know it's between a hundred oh sorry with the between a hundred oh sorry with the answers like the answer will fit in long answers like the answer will fit in long or 64 bit or 32 bit and debate the other or 64 bit or 32 bit and debate the other one too it is usually something that one too it is usually something that they would tell you but anyway so given they would tell you but anyway so given that or set this is that's why this that or set this is that's why this dynamic programming problem is a way dynamic programming problem is a way straight for it dynamic program in this straight for it dynamic program in this sense sense and then it's just about coating it up and then it's just about coating it up so I'm wearing do not yeah okay and so I'm wearing do not yeah okay and there's a easy optimization that I'll there's a easy optimization that I'll talk about but I'm not gonna do yet I did last time so maybe I should do it mmm I've been trying to do it more okay center corner to do the one and then I just found checking and now we just I just found checking and now we just have to return no maybe it's off by one have to return no maybe it's off by one out sometimes I walk by one on these out sometimes I walk by one on these things good oh yeah oops things good oh yeah oops would I get deep oh yeah let's try a real number for kicks oops we're in Python I think I don't have to kill but it's just in other languages a little weird that they don't specify especially in JavaScript for example see there's a very long number and I guess they don't have an expected result because it crashes on it that's watch I Mississippi case ok cool yeah so watch I Mississippi case ok cool yeah so again this is very straightforward but again this is very straightforward but that's how I kind of think about this that's how I kind of think about this problem and it these things are kind of problem and it these things are kind of fun to kind of think about because it fun to kind of think about because it comes up in random places as well oh comes up in random places as well oh yeah the other thing that I would say a yeah the other thing that I would say a very common thing is to refactor this is very common thing is to refactor this is that well this is a dynamic program well that well this is a dynamic program well obviously of M times of M times n and obviously of M times of M times n and with all of em times and space as well with all of em times and space as well but the thing is you actually reduced but the thing is you actually reduced this 200 of min of M or n by noticing this 200 of min of M or n by noticing that if you look at this away you that if you look at this away you actually you know which is this array actually you know which is this array here you essentially pretty much right here you essentially pretty much right so now 2 will always look at you know so now 2 will always look at you know one and this one and so forth and some one and this one and so forth and some domed we will go get the 2 and the 1 and domed we will go get the 2 and the 1 and sum them and so forth and then now like sum them and so forth and then now like we go all the way down and then now for we go all the way down and then now for some of 4 and 5 say well the thing is at some of 4 and 5 say well the thing is at the for you notice that you only add 2/3 the for you notice that you only add 2/3 under one way so actually in this entire under one way so actually in this entire well let me add some data tots in this well let me add some data tots in this entire world entire world you only care about the results from the you only care about the results from the previous world right so this will only previous world right so this will only calculate itself in this flow so calculate itself in this flow so actually if you kind of take advantage actually if you kind of take advantage of that you could actually just get rid of that you could actually just get rid of all these stuff well not get written of all these stuff well not get written I mean maybe get rid of it but you could I mean maybe get rid of it but you could you don't have to think you don't need you don't have to think you don't need that memory to stay around and that's that memory to stay around and that's you need to look up stuff for whatever you need to look up stuff for whatever reason in the future but but yeah so you reason in the future but but yeah so you notice that then actually you only need notice that then actually you only need to keep track of the last two rows or to keep track of the last two rows or the current world and the previous well the current world and the previous well so you can actually do that in linear so you can actually do that in linear space as well because yeah it's like you space as well because yeah it's like you know when this is done then you just know when this is done then you just keep track of the new one and so forth keep track of the new one and so forth way anyway so that's good how you solve way anyway so that's good how you solve this was I would say for a dynamic this was I would say for a dynamic programming problem this is way fun um programming problem this is way fun um this is a way one of they were more this is a way one of they were more fundamental ones I should definitely get fundamental ones I should definitely get to get familiar with and I heard that to get familiar with and I heard that Google was still asked a lot time before Google was still asked a lot time before grandpa I actually went into Google for grandpa I actually went into Google for a long time but uh but that's what I've a long time but uh but that's what I've heard heard but you know the way interviews I feel but you know the way interviews I feel like people have not been asking dynamic like people have not been asking dynamic programming well so so yeah maybe it's a programming well so so yeah maybe it's a but yeah we've want to go Google I guess but yeah we've want to go Google I guess you should study timing programming oh you should study timing programming oh yeah yeah that's why I have for this one
2024-03-20 10:53:51
62
https://i.ytimg.com/vi_w…xresdefault.webp
62. Unique Paths (Leetcode Medium)
PiPXuZN1O3U
welcome to code map today we are going to discuss daily record challenge find to discuss daily record challenge find the highest altitude the highest altitude now let's discuss the question there is now let's discuss the question there is a biker going on a road trip the road a biker going on a road trip the road trip consists of n plus one points at trip consists of n plus one points at different altitudes the biker status different altitudes the biker status strip at point zero with Attitude equal strip at point zero with Attitude equal to zero to zero you are given an integer array gain of you are given an integer array gain of length n uh where gain I is the net gain length n uh where gain I is the net gain in the altitude between point I I Plus in the altitude between point I I Plus 1. where I is between 0 and M 1. where I is between 0 and M you have to return the highest altitude you have to return the highest altitude of a point of a point let's understand it with a given example let's understand it with a given example test case in this example this case we have given again array which is minus five one five zero minus seven another question it is given that the biker is starting with zero again so at the beginning gain is zero now now when the backer will be at the point zero so it's its gain will be 0 minus 5 which is which is minus 5 and here also we can which is minus 5 and here also we can say it is -5 say it is -5 now when the biker will be at point one now when the biker will be at point one at point one this gain is 1 so it's so at point one this gain is 1 so it's so its current gain will be minus 5 its current gain will be minus 5 minus 5 plus 1. which is equal to minus minus 5 plus 1. which is equal to minus 4. and here we can see that it is minus 4. and here we can see that it is minus 4. 4. now current we have current gain we we now current we have current gain we we have current gain ah current gain have current gain ah current gain current again as minus 4. now current again as minus 4. now when the marker is when the marker is when the vehicle will reach 0.2 when the vehicle will reach 0.2 0.2 which is 5. 0.2 which is 5. so current again will become minus 4 so current again will become minus 4 minus 4 plus 5. minus 4 plus 5. minus 4 plus 5 which is equal to 1 and minus 4 plus 5 which is equal to 1 and we can see that here it is 1. we can see that here it is 1. now when the biker will move at the now when the biker will move at the point three point three the gain is 0 so 1 plus 0 the gain is 0 so 1 plus 0 equal to 1 so the current gain will be 1 equal to 1 so the current gain will be 1 and here we can see that it is one and here we can see that it is one and when the biker will be at the point and when the biker will be at the point 4 his gain is -7 so the current gain 4 his gain is -7 so the current gain will be 1 minus 7 will be 1 minus 7 which is equal to which is equal to -6 -6 and we can see that it is minus 6. and we can see that it is minus 6. now let's this is the code uh here I have taken a variable risk which will which will show the highest highest among all the altitudes and I have taken a current gain which will restore the current gain and I have run and I have run a loop to iterate through the gain array and I have calculated the current gain at every every iteration and I have maximized the current again and my result is in the resultant address variable and I have returned that now we can see that our code has now we can see that our code has successfully passed all the test cases successfully passed all the test cases thank you for watching
2024-03-20 17:48:00
1,732
https://i.ytimg.com/vi/P…axresdefault.jpg
Find the Highest Altitude | leetcode 1732 | Array Coding Questions
OHerBVV3jhs
hello everyone today we are going to calculate money in lit code bank which calculate money in lit code bank which is a problem which is a Del quing of is a problem which is a Del quing of today so uh basically what is happening today so uh basically what is happening is that you have a certain number of day is that you have a certain number of day which is the number of input n and then which is the number of input n and then from that input you are supposed to from that input you are supposed to compute the total amount that has been compute the total amount that has been saved in the bank given that uh the bank saved in the bank given that uh the bank is the money is safe given a process is the money is safe given a process that can be described as follow every that can be described as follow every day you are going to put like $1 day you are going to put like $1 plus what you put yesterday so if today plus what you put yesterday so if today I put $1 tomorrow I'm going to put two I put $1 tomorrow I'm going to put two and after tomorrow I'm going to put and after tomorrow I'm going to put three and so on but there's a little three and so on but there's a little trade at the end of the week I'm going trade at the end of the week I'm going to restart that means after 7 I'm not to restart that means after 7 I'm not going to put eight but I'm going to put going to put eight but I'm going to put $1 more than what I put in the first the $1 more than what I put in the first the last first week so this is the first last first week so this is the first week I put $1 here so at the end of this week I put $1 here so at the end of this week I'm going to week I'm going to put two instead of instead of one so I'm put two instead of instead of one so I'm not starting back with one I'm starting not starting back with one I'm starting back with with two so um this is how the back with with two so um this is how the thing is actually arranged now the goal thing is actually arranged now the goal is to find out how is to find out how many uh what is the the total amount we many uh what is the the total amount we are going to have at the end of the are going to have at the end of the process that means at the end of the and process that means at the end of the and days so for you to understand better of days so for you to understand better of what we are doing let's say you have what we are doing let's say you have four weeks the first week you are going four weeks the first week you are going to put 1 2 3 4 5 6 7 the second week to put 1 2 3 4 5 6 7 the second week you're going to start back at two that you're going to start back at two that is 1 + 1 and then you are going to have is 1 + 1 and then you are going to have the Siri 2 3 until 8 and so on and at the Siri 2 3 until 8 and so on and at the end of the day you are going to have the end of the day you are going to have the total amount of the the money you the total amount of the the money you need to keep so need to keep so um giv this process how are we going to um giv this process how are we going to solve it so my Approach of solving this solve it so my Approach of solving this will be as follow will be as follow I first so my Approach of solving this will be a follow I will go through two approaches the first one will be based on the exact description of what has been said here so I'm going to have a Conta which is going to move from one till 7 and once we are at at 7 we are we are just going to restart our cter but not with zero but with the previous day plus one so that is what I'm going to to implement here so uh for me to implement that the first thing I need to do is to have like a result here my result is actually zero I'm going to return the result here and here so I have the number of week uh the here so I have the number of week uh the number of week starting at zero and I number of week starting at zero and I have the days which is starting at one have the days which is starting at one right and then I will say the end of the right and then I will say the end of the end of the process is end of the process is actually the day actually the day where the number of day will be equal to where the number of day will be equal to n and since I'm going to restart the n and since I'm going to restart the number of day after each week I need to number of day after each week I need to compute the total number of day a little compute the total number of day a little bit different here so I need to say week bit different here so I need to say week * 7 plus d so this is a number of D so * 7 plus d so this is a number of D so while this is actually less or equal to while this is actually less or equal to N I will say a day ++ because I'm N I will say a day ++ because I'm incrementing my number of days and then incrementing my number of days and then what I'm going to do here is I'm going what I'm going to do here is I'm going to say result plus equal to day plus to say result plus equal to day plus week so what I'm saying is that after week so what I'm saying is that after each operation I need to add the number each operation I need to add the number of the the value of the number of day to of the the value of the number of day to the total sum that means at this step the total sum that means at this step I'm going to say okay my total s will I'm going to say okay my total s will take the prev value that was 0 + 1 at take the prev value that was 0 + 1 at this step it will take the pr Value Plus this step it will take the pr Value Plus two and so on but guess two and so on but guess what actually I'm doing what I'm doing what actually I'm doing what I'm doing here is actually the same thing that I'm here is actually the same thing that I'm doing here just that for each of those doing here just that for each of those value I'm adding + one + one + one why value I'm adding + one + one + one why because because we are on the week two because because we are on the week two we are on the week two so what you can we are on the week two so what you can do is that you can see keep adding the do is that you can see keep adding the number of day day day day but just number of day day day day but just adding the number of week so you can adding the number of week so you can directly handle the fact that we have directly handle the fact that we have different value for different weeks so different value for different weeks so that's what I'm doing here now the that's what I'm doing here now the question is how do I take care of the question is how do I take care of the number of day how do I change the value number of day how do I change the value to make sure that it is actually to make sure that it is actually accurate with the process you're going accurate with the process you're going to say okay if D is actually equal to to say okay if D is actually equal to eight that mean is the beginning of a eight that mean is the beginning of a new week you're going to say okay day we new week you're going to say okay day we take one and week take one and week Plus+ so whenever we are we are Hing the Plus+ so whenever we are we are Hing the end of or whenever we are starting a new end of or whenever we are starting a new we we are saying okay we are going to we we are saying okay we are going to restart the number of day so we are restart the number of day so we are still going to have 1 2 3 6 7 but we are still going to have 1 2 3 6 7 but we are going to increment the number of week so going to increment the number of week so that is why we are titing the number of that is why we are titing the number of day and the number of week here so let day and the number of week here so let us run this and see what it submit so uh we have everything submit so uh we have everything green uh so another approach of solving green uh so another approach of solving this will be uh based on artic formula this will be uh based on artic formula we are going to L the fact that the the we are going to L the fact that the the saving process is been performed using a saving process is been performed using a progression and using that we are going progression and using that we are going to compute the value that has been saved to compute the value that has been saved using mathematical formula so what will using mathematical formula so what will it look like you're going to use this it look like you're going to use this which is saying that if we want to which is saying that if we want to compute the sum of element following an compute the sum of element following an arithmetical formula we can directly use arithmetical formula we can directly use the first element plus the last element the first element plus the last element time the number of element divided by time the number of element divided by two and we are going to have the sum of two and we are going to have the sum of the element so this is what we are going the element so this is what we are going to use to kind of Leverage The the fact to use to kind of Leverage The the fact of using arithmetical formula to have of using arithmetical formula to have this solution so what will it look like this solution so what will it look like this is our things the first thing here this is our things the first thing here is to find out what is the Ser right and is to find out what is the Ser right and then you can see that during full weeks then you can see that during full weeks we have uh like static element like this we have uh like static element like this we have 1 2 3 2 7 now the next week we we have 1 2 3 2 7 now the next week we have again the the same value 1 2 3 till have again the the same value 1 2 3 till 7 till 8 this time but we are going to 7 till 8 this time but we are going to we can transform this into the 1 2 3 we can transform this into the 1 2 3 until 7 plus 1 one one one year and for until 7 plus 1 one one one year and for the third week we can transform it into the third week we can transform it into the same year plus 2 2 2 2 two year the same year plus 2 2 2 2 two year which will always give you like the 28 which will always give you like the 28 for the first week plus Z for the second for the first week plus Z for the second week 2 with 28 + 7 and 28 + 14 so this week 2 with 28 + 7 and 28 + 14 so this can be computed with with number of week can be computed with with number of week time 28 which is the static value and time 28 which is the static value and here we have a Ser which is actually here we have a Ser which is actually 074 which can be see as a Ser of 7 * 074 which can be see as a Ser of 7 * 012 so this is a 012 so this is a series now that you able to figure out series now that you able to figure out this so how do we compute this so let me this so how do we compute this so let me just grab you my solution so uh you have just grab you my solution so uh you have to compute the number of week we need to to compute the number of week we need to have the total number of week and then have the total number of week and then we need to have the remaining days the we need to have the remaining days the remaining days we are going to use it remaining days we are going to use it for for this one all right for for this one all right so um now to compute the the amount save so um now to compute the the amount save in in Full full days we are going to in in Full full days we are going to have total full week which is actually have total full week which is actually the 28 time number of week that is what the 28 time number of week that is what we said because we have 28 28 28 and we said because we have 28 28 28 and then now we are going to compute the then now we are going to compute the remaining year by using the formula we remaining year by using the formula we just we just see here that means the just we just see here that means the first element plus the last element time first element plus the last element time the number of element ided by two the number of element ided by two so in this scenario our first element is so in this scenario our first element is actually zero our our last element is actually zero our our last element is actually the number of we minus one and actually the number of we minus one and the number of element is actually equal the number of element is actually equal to the number of we so that's what we to the number of we so that's what we are going to use here we have our first are going to use here we have our first element which is zero we have our last element which is zero we have our last element which is actually number of we element which is actually number of we minus one we have our number of element minus one we have our number of element which is actually number of we that we which is actually number of we that we going to divide by two so this is the going to divide by two so this is the formula Computing uh this ser and don't formula Computing uh this ser and don't forget that we put seven in Factor here forget that we put seven in Factor here so we just need to reuse this seven here so we just need to reuse this seven here time 7even so this is actually the time 7even so this is actually the formula that will have us to compute the formula that will have us to compute the total number of week The Total Money Sav total number of week The Total Money Sav during full weeks now we also have a during full weeks now we also have a total remaining how do we compete the total remaining how do we compete the total remaining so the total remaining total remaining so the total remaining here we can see that is actually for here we can see that is actually for this specific case will be 4 + 5 + 7 this specific case will be 4 + 5 + 7 which is still kind of 1 + 2 + 3 because which is still kind of 1 + 2 + 3 because that is actually the starting pattern of that is actually the starting pattern of saving but depending on the week we are saving but depending on the week we are going to have a certain amount that has going to have a certain amount that has been added so if you have three weeks been added so if you have three weeks that means the four week each value are that means the four week each value are going to be like the normal saving plus going to be like the normal saving plus the number of week here we are on week the number of week here we are on week four the number of full weeks I mean four the number of full weeks I mean here we are on week four we had three here we are on week four we had three full weeks that means here this one is full weeks that means here this one is going to have the normal one plus three going to have the normal one plus three because we had three full weeks and the because we had three full weeks and the same year so this is actually the Ser same year so this is actually the Ser year and for you to have the total value year and for you to have the total value you can just reuse this the first you can just reuse this the first element is one plus the number of week element is one plus the number of week the last element is actually uh the the last element is actually uh the number of full weeks number of full weeks plus the uh the number of week and then plus the uh the number of week and then the and then uh yeah that's the first the and then uh yeah that's the first element that's the last element so how element that's the last element so how how can you solve this you're going to how can you solve this you're going to say the first first element is one plus say the first first element is one plus the number of week the last element is the number of week the last element is uh the remaining days I mean the uh the remaining days I mean the remaining days the remaining days is remaining days the remaining days is here number of remaining days actually here number of remaining days actually the remaining days plus the number of the remaining days plus the number of week and then the number of element is week and then the number of element is actually the remaining day so we have actually the remaining day so we have the first element number of one plus the first element number of one plus number of week last element number of number of week last element number of plus remaining there and the number of plus remaining there and the number of element is remaining there divid by two element is remaining there divid by two this is the formula so this will this is the formula so this will actually give you the value of this and actually give you the value of this and now that you have this you just need to now that you have this you just need to return the the the total full plus the return the the the total full plus the total remaining which is actually this total remaining which is actually this so let us run this and see what it gives so it's working really strange that the I will say um the big of one solution is per performing less better than the big of end solution so yeah this a woring thing so um I hope it was interesting for you going through this I hope you were able to learn something so don't forget to like share and subscribe
2024-03-20 15:24:43
1,716
https://i.ytimg.com/vi/O…axresdefault.jpg
1716. Calculate Money in Leetcode Bank: Two approaches
XXTYgC8xrMY
Hello and welcome tool again this is Shaur Awasti and today we will talk about a and today we will talk about a medium level problem of lead code which is bit wise and medium level problem of lead code which is bit wise and of number range so basically what is in it, of number range so basically what is in it, in this we are given left and right and in this we are given left and right and what is we have to tell that in what is we have to tell that in What will be the end of all, like suppose five and six and What will be the end of all, like suppose five and six and sane, what will be the answer of all three, sane, what will be the answer of all three, okay, so we cannot do it by putting a normal for loop okay, so we cannot do it by putting a normal for loop because that TL will give us because that TL will give us okay, so if we want to see a little optimized code, okay, so if we want to see a little optimized code, then we have to do this. then we have to do this. then we have to do this. can do this with the help of bit manipulation, so if we move towards enter, if we look carefully, one thing that we can see is that brother 2 and f if there is a range and we have If our range is from two to sorry, two and seven, we have written all the elements from two to seven in binary representation. After that, if we look carefully, a pattern follows when we write the binary representations If we keep If we keep If we keep looking at the elements, if we follow the continuous elements, looking at the elements, if we follow the continuous elements, then a pattern follows i.e. zero then a pattern follows i.e. zero index P means the one which is the highest, LSB is the least index P means the one which is the highest, LSB is the least significant bit. If we look at it, it follows the pattern of one, i.e. alternate one to alternate, zero i.e. zero then one then zero i.e. zero then one then Zero then one then zero then and so on Zero then one then zero then and so on is fine. Similarly if we is fine. Similarly if we look at the second one here then it follows and 00 and look at the second one here then it follows and 00 and then again 00. By doing like this the meaning of saying then again 00. By doing like this the meaning of saying is that every element is following a pattern is that every element is following a pattern meaning. meaning. meaning. bit follows a pattern, depending on which position it is in, so one approach we can think of is that there will be elements between Will not depend Answer: It Will not depend Answer: It Will not depend Answer: It depends only on the first and depends only on the first and last number, why did I say this because the last number, why did I say this because the first is the smallest, so its MSB first is the smallest, so its MSB i.e. the most significant bit, there are i.e. the most significant bit, there are chances that it will be at the lowest position, i.e. chances that it will be at the lowest position, i.e. look at it, it is found right here and look at it, it is found right here and look at it, it is found right here and most significant bit of our largest element has possibilities that it can be at the last position. You have to set the bit ahead of it, that is, you cannot find the bit set further ahead of this, anywhere above because this is your maximum i.e. largest element and this is Now we have Now we have Now we have understood that it depends only on the first understood that it depends only on the first and last number. Why did I say this and last number. Why did I say this because it follows a pattern and if we end each other because it follows a pattern and if we end each other then everything will be canceled then everything will be canceled out? Brother, if we look at this then rows and rows will be canceled out, out? Brother, if we look at this then rows and rows will be canceled out, right? right? right? we look at it like this, it becomes one, then again row is zero, then there is one, then this row will cancel out zero. If even one zero falls in the middle, which is bound to happen, If he is If he is If he is doing okay, then that zero will become zero, doing okay, then that zero will become zero, so the maximum dependency, we have so the maximum dependency, we have taken the example of our dependency from 2, taken the example of our dependency from 2, where is it coming from our two i.e. 1 2 where is it coming from our two i.e. 1 2 3 4 Let's assume like this, okay 3 4 Let's assume like this, okay 5, 6 and 7. 5, 6 and 7. 5, 6 and 7. I took it, for seven I took 1 2 3 4 and here there will be one more four then three, okay oh sorry, it will be like this okay, this is the representation of seven, so this is our Now if we look at this thing carefully, we can find out on the basis of these two, why did I say this because the most significant bit will It is It is It is significant that the largest element will be the significant that the largest element will be the highest i.e. no middle highest i.e. no middle element will be found in which father one element will be found in which father one is found ahead or father one is found here ahead is found ahead or father one is found here ahead i.e. no sorry, no middle element will be i.e. no sorry, no middle element will be found in found in which the most significant bit is found before it. which the most significant bit is found before it. which the most significant bit is found before it. means here also zero is present and no other smaller number will come in between it and in this case no bigger number will come in between i.e. it cannot go beyond this. Okay, so if we look here then In this, we will keep reducing the elements one by one from the right side, that is, we will keep shifting it to the right, we will keep shifting the left one to the right, that is, the right sen will also be shifted to the right one by one, until we have both left and right As we go, we shifted this As we go, we shifted this As we go, we shifted this element, this element disappeared, this element shifted, this element element, this element disappeared, this element shifted, this element shifted, okay again shifted, okay again this element or element shifted, this element or element shifted, now we have the remaining left and now we have the remaining left and right i.e. 00, both of them are equal, so we right i.e. 00, both of them are equal, so we In the while loop we will write while left is not equal to right, what will we do till then we will write while left is not equal to right, what will we do till then we will keep we will keep shifting left to right, ok one by one shifting left to right, ok one by one and also keep shifting right to right one by one until we become similar as soon as It will be similar and It will be similar and It will be similar and we will come out of the loop, okay we will come out of the loop, okay and one we will put a counter, one we will put a counter and one we will put a counter, one we will put a counter and as many times as we are shifting, we will take a counter so that we can come back to our position, okay ultimately When both are equal, When both are equal, When both are equal, meaning whether you lift left or right, meaning whether you lift left or right, now the two that are left here, when the now the two that are left here, when the right shift is completed and the two that are right shift is completed and the two that are left at the end, will be equal, then either left at the end, will be equal, then either all the zeros will be zero or all the ones will be one and all the zeros will be zero or all the ones will be one and Either all the forests will be one, take one example Either all the forests will be one, take one example and understand the case of one, okay take one more and understand the case of one, okay take one more example let's suppose here 1 1 1 1 0 1 example let's suppose here 1 1 1 1 0 1 0 1 and this 1 1 1 1 10 10 If we see in this then 0 1 and this 1 1 1 1 10 10 If we see in this then right shift this right shift this right shift this disappeared, this row has come here This element has disappeared here, this row has come Okay, this is our left, this is our right, again it is not equal The row zero has disappeared, this row has come here from here again Look again, still not equal Right again it disappeared and one more Right again it disappeared and one more Right again it disappeared and one more zero zero came in the right sides means three zero zero came in the right sides means three zeros are totaled. Again not equal. zeros are totaled. Again not equal. Gaped again one more zero got inserted. Okay so Gaped again one more zero got inserted. Okay so now four zeros have come from here and the next now four zeros have come from here and the next four four four ours becomes left and right, this becomes left, this becomes right, so if we look here, now we have equal brother, left is equal to right, okay, there are four set bits There are four sets of bits in it, There are four sets of bits in it, There are four sets of bits in it, so everything is equal here, so now that it is equal, so everything is equal here, so now that it is equal, in this case, if we in this case, if we look carefully, when it is equal, then the elements that are left on our right look carefully, when it is equal, then the elements that are left on our right side, that is, on the left side, side, that is, on the left side, which we have not shifted yet. which we have not shifted yet. Right, all those are equal, either all va's or Right, all those are equal, either all va's or all ji's are ok, so what to do in this case all ji's are ok, so what to do in this case when these two come, in this case we can take when these two come, in this case we can take any one, take left or right, take it at any one, take left or right, take it at the end, this is what our answer is left. the end, this is what our answer is left. But to bring it back to its position, But to bring it back to its position, i.e. the number of times we have taken the count here, the i.e. the number of times we have taken the count here, the number of times we are shifting, number of times we are shifting, that is the number of times we are taking the count, after that we will that is the number of times we are taking the count, after that we will count minus the number of times and the same number of times count minus the number of times and the same number of times we have shifted right. we have shifted right. we have shifted right. we will shift left or left let us suppose that we picked up the left and from there we shifted it left with that much time i.e. all the elements which were there returned to their positions and 0 0 00 came here 0 When we come here, now When we come here, now When we come here, now what was the value of left, four times 0, four times one, then four what was the value of left, four times 0, four times one, then four times 0, four times one, back, how times 0, four times one, back, how many counters did we have, brother, here one doti cha many counters did we have, brother, here one doti cha four, we shifted right, so four four, we shifted right, so four times we did this. times we did this. come will come. come will come. Okay, so ultimately this will be our Okay, so ultimately this will be our answer. Okay, so this thing is answer. Okay, so this thing is clear. Once we will code this thing and clear. Once we will code this thing and see, it will be a little better clear to So first of all, we create a counter in this, So first of all, we create a counter in this, So first of all, we create a counter in this, okay, after that we okay, after that we write the while loop, while left is not equal to right write the while loop, while left is not equal to right inside, what will we inside, what will we do, okay inside, till the time our do, okay inside, till the time our left is not equal to right, then in the left left is not equal to right, then in the left Once again we will Once again we will Once again we will update the value of right. Right shift update the value of right. Right shift is ok. Now this thing is cleared. Then we will keep incrementing our count. After that finally we will answer when left or wide Or right, Or right, Or right, both will be equal to each other, left and right, both will be equal to each other, left and right, then after that keep your left in return then after that keep your left in return or keep right, to bring it back to its position, we will or keep right, to bring it back to its position, we will hit left shift again, hit left shift again, how many number of times count the number of times, how many number of times count the number of times, we will return it we will return it and see. and see. and see. OK, here I have missed the semi colon. OK, all three items are passed. Once OK, all three items are passed. Once submitted, it is fine. Thank you so much for watching this video. If you liked the video, then See you See you See you in your next video till then in your next video till then bye
2024-03-22 09:40:05
201
https://i.ytimg.com/vi/X…axresdefault.jpg
Leetcode 201. Bitwise AND of Numbers Range || Bit Manipulation || POTD (21Feb2024)
YfCvAgDH6lg
um hello so today we are going to do this problem called check if string is a this problem called check if string is a prefix of array prefix of array so the problem says we get a string s so the problem says we get a string s and then array um of string words and then array um of string words and we want to determine whether s is a and we want to determine whether s is a prefix of strings of orders okay and then we get an array that is a and then we get an array that is a string of words okay so this is array string of words okay so this is array and this is string s and this is string s and then and then i'm going to determine whether s is a i'm going to determine whether s is a prefix of words so [Music] s three tricks or four something like this okay what does that mean um so a string s is a prefix string of words if s can be made by in words in words for some positive k you know larger than for some positive k you know larger than the length okay so it seems like what this would mean is that we will get like a something like words that contains um some values like w1 something like this and we can say s is a concatenation of let's say s is a concatenation of let's say the first case strings in words right the first basically we concatenate right the first basically we concatenate the first k words the first k words k words and then example here we get a sequel to this and what is equal to that and we want to figure that out so so what this would mean here is that we so what this would mean here is that we get get um um so if we say so s can be a here is actually it's just like was zero plus uh plus words uh plus words yeah that's pretty much it's okay is yeah that's pretty much it's okay is equal to yeah i think we should yeah i think we should the problem is whether it's the problem is whether it's concatenating the first concatenating the first thing you can just check right thing you can just check right so here so here it can be made only by the it can be made only by the three last ones but not containing the three last ones but not containing the first one so first one so i think we can just keep checking right i think we can just keep checking right and until we reach the end so and until we reach the end so that would mean something along the line that would mean something along the line of uh in words uh in words right actually we can even do but i think we join join you can just join you can just join actually not in it starts with so actually not in it starts with so python string starts with python string starts with uh yeah i don't think that would work uh yeah i don't think that would work because if because if a portion of it is in one a portion of it is in one one um one one of the array values and one um one one of the array values and not in the not in the not the entire value then we'd have a not the entire value then we'd have a problem so we can do this so we'd have problem so we can do this so we'd have to say for to say for say w in words okay okay how can we name this so how can we name this so yeah let's call it c it's just a yeah let's call it c it's just a concatenation right so here we do c plus concatenation right so here we do c plus w w and then if s and then if s equal to c then return true equal to c then return true otherwise it's returned otherwise it's returned it's a very simple solution here okay so it seems like that one works what if i try one that kind of has what if i try one that kind of has just this right and just this right and this should not work it should be false okay cool so that works okay cool so that works um what if we try something um what if we try something can we do it in a better way can we do it in a better way um yeah i think we can so we can do um yeah i think we can so we can do actually just uh yeah i know i was thinking of doing maybe joining words like this but that won't really be we can't use any here uh because we want we can't use any here uh because we want to check to check the accumulation maybe we can use scan i the accumulation maybe we can use scan i think think um because cam gives us the um because cam gives us the the last result okay let's see python okay i want this functional uh the functional one um okay so definitely do the juice right so if i do if i take the list of words here that we have so that will be words equal to this and then if we try to do the juice here so for reduce we'll do some need to do something like reduce which would be x and y and then what we want is to and then we need to press words okay so and then we need to press words okay so this groups everything together the problem is i want to somehow be able to compare this but if s is this it should still return sounds like i need all the concatenation sounds like i need all the concatenation so i need this so i need this i need this and i need to check equal so i need this and i need to check equal so i need all the concatenation up until i need all the concatenation up until that point that point um so um so how can so we kind of need to so we kind of need to reduce with intermediary results accumulate maybe so ah okay so ah okay so i think this is potentially what we want okay so import you put accumulate okay and then what we need to do is call it and so the ira irritable is words and then the function that we want is this okay if we turn it into a list just to okay if we turn it into a list just to see the content see the content it's exactly what we want right it's exactly what we want right and then here we can check if any and then here we can check if any so here any is takes a miracle right so so here any is takes a miracle right so it can take actually this so any it can take actually this so any uh i need to first define s so s is uh i need to first define s so s is let's say this let's say this right and so what i need at this point is to any equal to s equal to s for w in this variable uh okay seems like there is a okay this so it says true because yes it does this should work so something like this okay so that seems like it works let's try to but i think so that works cool i think but i think so that works cool i think though we can make this i think there is though we can make this i think there is a plus a plus um in python uh python reference um in python uh python reference plus plus operator uh okay so i'm not sorry about that so python pass plus yeah somehow remember but so maybe it's what accumulates even does so maybe it's what accumulates even does by default by default yes yes yes so um actually accumulate does yes so um actually accumulate does concatenation by default both strings concatenation by default both strings because because if i do this and just show it as a list if i do this and just show it as a list then i get the same thing so it's even then i get the same thing so it's even better with just accumulates like this better with just accumulates like this and and we'll check if we find any and actually we just need to check if s is in this we don't even even need to do any so this is a lot simpler and that works so and that works so an even simpler solution is just as an even simpler solution is just as my weight my weight yeah i think this this is good good yeah i think this this is good good enough cool
2024-03-25 13:33:55
1,961
https://i.ytimg.com/vi/Y…D7KBj-VSHgHjt70Q
Leetcode 1961: Check If String Is a Prefix of Array
0TIgjS5b4w0
hey this is josh from leak dev where we try to explain programming questions as try to explain programming questions as simple and concise as we can simple and concise as we can today we'll be talking about the lowest today we'll be talking about the lowest common ancestor of a binary tree common ancestor of a binary tree according to leed code this question has according to leed code this question has been asked quite frequently by facebook been asked quite frequently by facebook amazon microsoft and just from my own amazon microsoft and just from my own personal experience i was actually asked personal experience i was actually asked this question multiple times this question multiple times in screening interviews and actual in screening interviews and actual on-site interviews on-site interviews so being able to know how to work with so being able to know how to work with trees will be a good thing to have in trees will be a good thing to have in your tool set given a binary tree we want to find the lowest common ancestor of the two given nodes in the tree so what that basically means is for example if we were given p equals five which is right here q equals one which is right here we return three because three is the in example two we're given five which is in example two we're given five which is right here right here and four which is right here the first and four which is right here the first common ancestor of them all common ancestor of them all is five itself because according to the is five itself because according to the definition of definition of the lca a node can be a descendant of the lca a node can be a descendant of itself itself so we return five for example too and so we return five for example too and then finally in example three then finally in example three we have a tree we don't have a picture we have a tree we don't have a picture of it but we can imagine of it but we can imagine a tree of one two which probably just a tree of one two which probably just like this three and one right here like this three and one right here and then we would just return the answer and then we would just return the answer one one 1 would be the root node and once again 1 would be the root node and once again a node can be a descendant of itself a node can be a descendant of itself one thing to note is that there are one thing to note is that there are several constraints to this problem several constraints to this problem specifically the important ones to know specifically the important ones to know is that all node values is that all node values are unique the p-value that we're given are unique the p-value that we're given is not the same as our q value is not the same as our q value and that p and q are guaranteed to exist and that p and q are guaranteed to exist in tree to solve this problem and all tree problems in general we'll use a recursive solution where we will traverse through the whole tray and find our lowest common ancestor taking a step back and looking at this problem at the high level intuitively it's pretty simple we want to do is we'll recurse our tree we start from one we go through this love child see if it's one of the notes that we're looking for and if it's not we go down look at its left child we find three and we know that it's one of the nodes that we're looking for to our two and then we recurse the right to our two and then we recurse the right tree tree and see that it is also one of the value and see that it is also one of the value that we're looking for q that we're looking for q and so we return something back up so and so we return something back up so maybe another true maybe another true in this example and then once we're back in this example and then once we're back at 2 and we finish recursing both at 2 and we finish recursing both its left child and its right child we its left child and its right child we can see both its can see both its childs and see that we have found our childs and see that we have found our answer answer and so we return ideally the problem we and so we return ideally the problem we want the specific node want the specific node but in this example let's say we return but in this example let's say we return true true and then we go back to one we recurse and then we go back to one we recurse it's right child it's right child and c5 5 isn't what we're looking for so we look at the left node which is null so we return say false and we also look at its right child which also is null so we also return false so because we haven't found the notes we're looking for in five we and then once we go back up to our and then once we go back up to our starting node at one starting node at one we we looked at the left node and we see we we looked at the left node and we see that we did find our ancestor so that we did find our ancestor so we returned some value back up and we returned some value back up and that would be how we would solve this that would be how we would solve this algorithm of course we can't just return algorithm of course we can't just return true or false because this problem wants true or false because this problem wants us to return the node that is our us to return the node that is our least common ancestor so we want to least common ancestor so we want to somehow return to in the previous example we bubbled up a true or false value to indicate whether or not we found the nodes and then we bubbled it back up to the top unfortunately that won't work because in this specific problem we want to return the exact node itself that would give us the answer however we can modify our algorithm a bit for example instead of returning a true or false value what if we return the node itself for example once we find the three node we return that node back up and once we find the for node we return that node back up too and then once we're at the least common ancestor and we identify that there are the it is the least common ancestor we can just return that node back up and eventually return that as an answer to be able to accomplish this algorithm i have a six step plan above now first let's clean this up like all three problems we usually iterate through the tree using recursion and we need to do recursion we need to define our base case and our recursive case so what is our base case we have two base cases the first base case is when we reach a null node that means we haven't found anything when that happens we just return null back up the second base case is what happens if we find one of the descendant nodes like three or four when that happens we return that node back up and finally we have a recursive case where we have an identified answer and we want to just continue iterating there's two parts to this first we want to just continuously explore the left and the right subtree of the node that we are currently on and then eventually we will hit those base cases that we talked about and we will return a node back up so three and four now at two while we're processing the left and right subtree if both of them are true we can just propagate the node two back up otherwise if one of them is actually a value and not a null we can just return that one otherwise return null so example in five we would return null because both of its subtree child is null and then we return no back up here and then finally at 1 we'll see that we have 2 and null so we just return 2 which would give us our answer there is one specific case in this algorithm that i didn't address that it handles perfectly though and that's what happens if we have a node that is a descendant of itself so for example if our value p equals two and q equals 4. how this play out in our algorithm because we would actually never process 4. so first let me clean this page up and then we'll do a quick run through right so what happened here is we go to 1 and then we go to two and then two would be one of our base case and then we just return it up however you might just wonder well didn't we just ignore four how do we know if two is actually the ancestor and the reason for that is because of one of the constraints in our problems specifically that p and q must exist at this point if we found one of our nodes and we turn that up that node already is guaranteed to be the common ancestor if we don't find any other nodes in the other sub tree of its parents so in this instance we would return two back up and then everything will play out the same we go to one one would then go to its right subtree which end would return and then finally back at one we compare and then finally back at one we compare two and null and then return two and null and then return two because it's the only non-no value two because it's the only non-no value and we would have our and we would have our least common ancestor and that's roughly least common ancestor and that's roughly the idea of how this algorithm works the idea of how this algorithm works let's get to the coding section let's get to the coding section in recursion problem i like to find in recursion problem i like to find define a private helper function define a private helper function because usually you need to pass in because usually you need to pass in different nodes in this instance it's different nodes in this instance it's all the same all the same but i'll create one anyways so to reiterate q and p is the nodes that we're looking for and node is the current node that we are at remember for all recursion problems we need to define our base case and our recursive case our base case is either if we hit a null node which means we haven't found anything or if we found one of the nodes and then when that happens we just return the otherwise in our recursive case when we otherwise in our recursive case when we are iterating through our node are iterating through our node we have to go all the way to the left we have to go all the way to the left child and see if child and see if there's a common node and the right there's a common node and the right child to see if that's a common node child to see if that's a common node and if both left and right is a common and if both left and right is a common node that means node that means our current node is the ancestor our current node is the ancestor otherwise we just return otherwise we just return the node back up and that's all for this algorithm very straightforward to code but not so easy to think about and actually implement however just like what we always do let's walk through our problem so first we start with our helper function and we pass in basically everything we were given so current value node is one one is not null nor is it any of the descendants that we're looking for so we enter a recursive case to do that we just explore our node.left and we just pass in p and q that brings us to node two node two also does not hit the base case so we enter our recursive case we check our left node which once again is now three now three actually is our base case so we just returned three back up back at two our left is now three but we're not done recursing yet the next thing we need to do is we need to recurse through our right node so we go to four and we find that four is one of our base cases it's actually equal to q so we turn four back up so we're back at two and our left node is 3 and our red node now we are at line 21. if both the left now we are at line 21. if both the left and the right node is not null that and the right node is not null that means we found the common ancestor and means we found the common ancestor and we can just return our node we can just return our node we return two back up to our we return two back up to our one node so we exit our left recursive one node so we exit our left recursive case case so in this instance let me just erase so in this instance let me just erase everything everything we are at two for our left node we are at two for our left node now we still need to explore our right now we still need to explore our right node so node so we go down there and we enter our we go down there and we enter our recursive case here recursive case here 5 is not null nor is it any of our 5 is not null nor is it any of our descendants so enter a recursive case descendants so enter a recursive case and just to reiterate where we are i'll and just to reiterate where we are i'll use blue to say this is the left and use blue to say this is the left and right value of our word node right value of our word node and i'll use green to say this is the and i'll use green to say this is the left and right value of our five node left and right value of our five node so r5 and we enter a recursive case so r5 and we enter a recursive case we go to left subtree which is null we go to left subtree which is null which is our base case which is our base case and then we turn null back up so that and then we turn null back up so that says null says null and then we do the same with our right and then we do the same with our right subtree which is also null subtree which is also null and because both of our nodes are null and because both of our nodes are null we don't enter our if statement we don't enter our if statement and so we go to our else case and so we go to our else case in this code we said that if left is not in this code we said that if left is not equal and null we return equal and null we return left otherwise we turn right in this left otherwise we turn right in this instance instance because they're both null it doesn't because they're both null it doesn't really matter and we just return no back really matter and we just return no back up up so null goes back up here so null goes back up here and we're back up here and now and we're back up here and now in our one node we have our calculated in our one node we have our calculated node write subtree value which is null node write subtree value which is null and then we follow the same exact logic and then we follow the same exact logic because both of them because both of them are not equal to something we don't are not equal to something we don't return the current node we're at return the current node we're at so instead we check if the left node or so instead we check if the left node or the is null the is null and then we want to check if one of our and then we want to check if one of our nodes is not null and then return that nodes is not null and then return that in this instance our left is not null in in this instance our left is not null in fact it's actually our ancestor fact it's actually our ancestor so we want to return that node back up so we want to return that node back up and so we just return and so we just return two which one will give us the answer to two which one will give us the answer to this question this question now let's submit our code pop this back up and there you go this is how you solve the least common ancestor problem the code itself once we look at it isn't horrible to write but once again this the thought process and reasoning involved to get to it it takes a quite a bit of a leap key to these tree prom is that we always want to explore all the nodes go to our base case or usually the bottom of our tree and then bubble our answers back up to the root node now if you consider this video helpful please consider hitting like button and maybe even hitting the sub button to get notified whenever we make daily updates otherwise i'll see you in the next video
2024-03-19 18:13:16
1,650
https://i.ytimg.com/vi_w…xresdefault.webp
Lowest Common Ancestor of a Binary Tree Leet Code Question Explained using Recursion
baRZ4EwtGjs
[Music] hello everyone welcome to coding decoded hello everyone welcome to coding decoded my name isja I'm working at sd5 at Adobe my name isja I'm working at sd5 at Adobe and after a very long time I have come and after a very long time I have come on to creating a new video because uh on to creating a new video because uh all the questions in the month of all the questions in the month of February seems like an easy question and February seems like an easy question and most of them were already solved by me most of them were already solved by me already and for those who were not I already and for those who were not I believe that you guys are capable enough believe that you guys are capable enough to solve those questions because the to solve those questions because the land up an easy category and today since land up an easy category and today since it was a medium question that I haven't it was a medium question that I haven't solved in the past so I thought let's solved in the past so I thought let's create a video and it will give me an create a video and it will give me an opportunity to interact with you so opportunity to interact with you so welcome guys welcome to day 19th of welcome guys welcome to day 19th of January lead Cod Challenge uh it's ex wait now it's 19th yeah day 19th of February sorry February lead Cod Challenge and the problem that we have in today is binary tree zigzag level order reversal the question says you need to perform the zigzag level order reversal of a binary tree and what does it mean it means that whenever you are moving from one level onto another uh the direction in which the BFS reveral has to be performed will change for example here you will be moving from left to right here you will be moving from right to left here you will be moving from left to right and the answer correspond that first is left to right then it's right to left then it's left to right so without further Ado let's quickly move on to the presentation and let's quickly get hold of the algorithm lead code 103 binary tree zigzag level on the traversal this question is a medium level question on lead code but the subscribers of coding decoded would feel that this is an easy question and for those who are new to coding decoded this video is primarily for them also in case if you have any doubt understanding this problem upart if you want to ask anything from me in general with respect to job opportunities internships placement please feel free to drop a message on the telegram group of coding decoded we have 2,000 developers working from all top notches in this group and we openly discuss problems that we face in day-to-day activity what's new happening in the industry Char GTP AIML stuff referrals layoffs and what not looking forward to seeing all of you there now let's get back to the problem I have taken a slightly longer example so that you guys get a good hold of the concept and here the root of the binary T that is given to us is 10 so the question says you need to perform the zigzag level order traversal moving ahead let's start the iteration and at the zeroth level we will move from left to right direction in the level order reversal in the next level which is level one we will move from right to left then we will move from left to right and then we will move from right right to left so definitely we will need a variable that will help us identify whether we are moving from right to left or left to right and the answer corresponding to this binary tree would be the first one would be 10 then we would be then we would have 0a 40 then we will have 7A 2A 1112 7A 2A 1112 78 and now comes the question how can we algorithmization in which you're doing the movement at any particular level so let's create a level a variable named left to right l t and let's initialize it to true because at the zeroth level we are moving from left to right let's start the iteration and as the basics of level order reversal what do we need we need a queue so let's get started and let's go ahead and create a queue what we're going to do we'll add add root into the Q what is the root element the root element happens to be 10 and we will initialize the left to right element to default value of true now we are done with the level order traversal L zeroth node the next thing that we are going to do is to pull this element out so this element is gone and uh what is the value of left to right the value of left to right happens to be true so we will simply build a new array and we will add 10 onto it awesome the first ele element of our answer is done let's move ahead and since we are removing this element what do we need to do we need to make an insertion of all its children into the Q so 40 and0 will be added onto the Q and remember we'll be adding the left element first followed by the right one so 40 get started followed by zero and this forms the boundary of the first level of the BFS reversal this is level zero of BFS reversal let's contining the process and remember since we are done with level zero of the BFS reversal what we going to do we'll be flipping this value up left to right now let's set it to false we are basically toggling this up and let's continue the process the next element that we have happens to be 40 40 gets extracted and it's time to build the second element of our answer and what do we see we see that the left to right element happens to be false so we'll simply make an insertion since it's the first element for 40 so 40 gets started and again since 40 is getting removed we will have to make an insertion for its children into the BFS get 7 comma 2 so 7 gets added followed get 7 comma 2 so 7 gets added followed by two so this is level two of the BFS reversal and let's continue the process now comes the important part where we will need the left to right variable the next element that we have happens to be zero and since 4 is gone let's delete it the next element that we have happens to be zero and since left to right element is false what we are going to do we'll make an insertion into our answer array at the starting node 0 gets added at the starting node instead of adding zero to the ending position we will be adding this zero to the starting node and this is the Crux of the problem U since we are removing Zero from the first level of our BFS traversal we should be adding its children into the BFS Q what element do we have as it children 112 So 112 gets added over and this element is gone now let's and this element is gone now let's continue the process and since we are continue the process and since we are moving to the next level we will be moving to the next level we will be toggling the value of the left to right toggling the value of the left to right variable so now again it gets set to variable so now again it gets set to true and we are done with the second true and we are done with the second element of our answer which is 0 comma element of our answer which is 0 comma 40 so let's proceed ahead and it's time 40 so let's proceed ahead and it's time to create the third element of for to create the third element of for answer uh the second level has three answer uh the second level has three elements in it 1 1 2 2 and 7 so let's elements in it 1 1 2 2 and 7 so let's pull out the topmost element from it pull out the topmost element from it seven gets seven gets extracted and uh since seven gets extracted and uh since seven gets extracted it doesn't have any children extracted it doesn't have any children so no insertion is made into the Q and so no insertion is made into the Q and let's add seven as part of our answer let's add seven as part of our answer now let's continue the process the next now let's continue the process the next element that we have happens to be two element that we have happens to be two so two gets extracted and does it have so two gets extracted and does it have any children yes it has one child which any children yes it has one child which is 78 so let's write 78 over here and since left to right variable happens to be true we will be making an insertion into an answer array at the end so two gets added at the end had it been the case where left to right variable would have been false we would have made an insertion at the start let's proceed ahead the next element that we have is 112 So 112 gets pulled out and it has 34 at its child so let's add 34 over here and let's add 1112 at the end of our answered answered element so in total we have three elements 7 2 1 1 2 and those are present in this particular order which is moving from left to right we are done with the third element of our answer and let's proceed ahead the next level would be three and since we are moving on to the next level let's let's set the the left false let's continue the process the false let's continue the process the first element that gets extracted first element that gets extracted happens to be 78 let's create the fourth happens to be 78 let's create the fourth element let's add 78 over element let's add 78 over here and it doesn't have any child so no here and it doesn't have any child so no insertion is made the next element that insertion is made the next element that we have is we have is 34 it also doesn't have any child so 34 it also doesn't have any child so this element would be inserted at the this element would be inserted at the starting point of our answer result why starting point of our answer result why at the starting point because our left at the starting point because our left to right variable happens to be false so to right variable happens to be false so let's write 34 at the let's write 34 at the start with this we have successfully start with this we have successfully iterated over all the elements that are iterated over all the elements that are present in our binary tree and in total present in our binary tree and in total the answer corresponds to 10 040 72112 the answer corresponds to 10 040 72112 3478 and this is what the question 3478 and this is what the question expected us to return so without further expected us to return so without further Ado let's quickly transform this Ado let's quickly transform this algorithm into code and I'll exactly algorithm into code and I'll exactly follow the same steps as I just talked follow the same steps as I just talked here the first thing that I've have done here the first thing that I've have done here is to create a list of list of here is to create a list of list of integers that will help me return the integers that will help me return the answer result in case my root is null I answer result in case my root is null I simply abought the process and return simply abought the process and return the ANS otherwise I go ahead and create the ANS otherwise I go ahead and create a BFS Q I add my root onto it I create a a BFS Q I add my root onto it I create a variable named left to right and variable named left to right and initialize it to true the typical we of initialize it to true the typical we of lighting any BFS reversal Q dot is not Q lighting any BFS reversal Q dot is not Q is not empty I extract the size while is not empty I extract the size while size minus minus is greater than zero I size minus minus is greater than zero I extract the topmost element and here I extract the topmost element and here I have created another subl list that will have created another subl list that will help me create the answer at that help me create the answer at that particular level if my left to right particular level if my left to right happens to be true what do I do I make happens to be true what do I do I make an insertion towards the end of the subl an insertion towards the end of the subl list in case my left to right variable list in case my left to right variable happens to be false I make the insertion happens to be false I make the insertion at the starting point of my subl list at the starting point of my subl list and this is again typical way of writing and this is again typical way of writing the BFS reversal if my head dot left is the BFS reversal if my head dot left is not null I add it into the cube my head not null I add it into the cube my head dot right is not null I add it into the dot right is not null I add it into the cube and once I've done with iterating cube and once I've done with iterating over a particular level in my BFS over a particular level in my BFS reversal I add my sublist on onto the reversal I add my sublist on onto the result and I toggle the value of left to result and I toggle the value of left to right once I'm out of the while loop right once I'm out of the while loop outer while loop I simply return the outer while loop I simply return the result so let's go ahead and submit this up guys in case you haven't still learned this BFS reversal template it's high time you do it because this is same template that is used in all the BFS questions also it really helps in the graph problems so I would urge you guys to learn this as a temp template and you will be using it everywhere apart from this if you're interested in solving more questions of the tree concept then you can check out coding decoded SD preparation sheet here it includes all the important questions from interview's perspective so if you have an important interview plann very soon and you want to go through the entire revision process then this sheet would act as your Bible so go ahead and select trees for the BFS traversal concept and here you will see the questions that are mared with double sign as a must do before an interview and you can try all these questions using the BFS technique also in case you get stuck in any of the question then here is the video solution attaching it Link in the description below so do check this out with this let's wrap up today's session I hope you thoroughly enjoyed it if you didn't please don't forget to like share and subscribe to the channel thanks for viewing it have a great day ahead and I'll see you tomorrow with another fresh
2024-03-21 10:15:18
103
https://i.ytimg.com/vi/b…axresdefault.jpg
Leetcode 103 Binary Tree Zigzag Level Order Traversal | Amazon Google | SDE Sheet below
README.md exists but content is empty.
Downloads last month
32

Data Sourcing report

powered
by Spawning.ai

No elements in this dataset have been identified as either opted-out, or opted-in, by their creator.

Models trained or fine-tuned on kreimben/leetcode_youtube_cc

Collection including kreimben/leetcode_youtube_cc