Abhinav provides a clear and efficient breakdown of algorithmic logic, turning complex DP and O(1) problems into accessible insights. It is a practical guide for those looking to move beyond brute force toward more elegant competitive solutions.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
A-D Leetcode Biweekly Contest 187 Editorials | Solutions | Abhinav Awasthi
Added:Um hello everyone.
I hope I am clearly audible and visible.
Let me just check once.
>> Hello everyone.
>> Yeah I think yeah I'm clearly audible and visible everyone. So hello uh today in this particular session we will uh discuss about the bi-weekly contest 187.
I think the video alignment is very low.
Let me fix the video alignment a little bit. Uh let me increase it a little bit. Now this looks good.
Yeah.
So save.
Okay. So, I hope now my layout looks good. So, today we'll be discussing about bi-weekly contest 187. It just happened uh 1 hour back and uh I think half an hour back, right? 9:30. Yes. And uh it's May we will discuss about we will discuss about in this contest mainly about the all the four questions of the contest questions. discuss contest questions. The first three questions were I will say comparatively easy mostly in a lead code contest. They were not so difficult but they were good questions.
Uh you guys can let me know questions in your uh in your contest. Okay. We'll discuss all of them one by one. Uh let's start from the very first problem named rearrange a string to avoid character pair. Okay. So this was a very basic and easy problem. Obviously the first and second problems of contest mostly easy contest you are given with distinct characters lowerase X or Yar same Y characters should appear before the X characters. Okay. To how we were doing it to up it was a very straightforward.
So if there are any Y characters in the string, we will count them. We will count the X characters and we will place the Y characters before the X character and the remaining string we can keep after that. Let me give you an example.
Let's say that your string is uh a minute.
[clears throat] Let's say that your string is abin okay your string is above. Now in this string if you have to solve the same problem. So what you will actually do a and according to them we will say that it is present actually how many times it is present for two times.
It is present two times. Sorry.
X character count and Y count X or Y.
Okay. So you will say the X character is present two times. Y character is present one time. That is what we count.
We count. Okay. M it is present once and X A is present twice. We have to do what? H first we have to print all the Y characters then X characters then the remaining string. So what I will do is I will first print all the character X means uh sorry all the characters Y which is N then I will print the the character X which is A and then the remaining string will come into picture.
So this is how we are solving the problem. So first of all count based on the count to first problem rearrange string. It was a very basic problem. I hope first problem clear is this first problem clear to everyone.
The first problem okay which is rearrange a string to avoid character pair.
Uh it was a very basic first problem is mostly easy order of end time complexity problem was there. Okay. Uh great chan.
So that is that is clear. Okay. So I will also actually put here uh which problem we are solving. So currently you are solving the first problem and I hope that the first problem is clear to everyone. The link to all the solutions of each problem I will uh you know send in the chat but still you guys can just check it from here. Okay. So this is how the problem looks like. Now let's move forward to the next problem. Uh can we go to next problem guys?
So next problem is maximum value of an alternating sequence. Look this was a very good problem. Okay I so actually I got two wrongs in this problem. You will see I made two wrong summations for the second problem because of this corner case of n equal to 1. I will actually tell you about this corner case also.
There were two corner cases that we need to uh take help here. So we'll discuss about both of them. Uh let me just add the banner of this next problem.
Huh? So now we'll discuss about the question maximum value of an alternating sub uh sequence. Okay. What this problem is saying problem numbers? Okay, let me explain this problem to you.
Let me explain this problem to you.
So this is the problem statement. Oh no.
Let me take the problem. We have three numbers N, S and M. Okay, great. So we have three number N, S and M. Now what you have to do is you have to start your sequence from S. So your sequence first. So you have to create a sequence where your sequence should always start from S and it should be a alternating sequence. What do you mean by alternating? So alternating means that if your sequence starting from X next step is should go down means it should be decreased then the value should be increased then it should be value should be less. So every value so after S the next value of S should be more than S then it should be S_sub_2 should be less than s_sub_1 then s3 should be greater than s_sub_2 and so on. This is what alternating alternating sequence is. And vice versa, it can also go like this.
Okay. This is how you have to create a sequence. Up. There is one more thing what you have to find and they are saying is the difference between any two number of the sequence cannot be more than m. So difference between them will always be less than equal to m. That is one thing. So now what you need to find is that if n is given to you. So in based on n how much maximum element what will be the maximum element that can appear in this sequence based on the given conditions. So based on the given condition what will be the maximum element that is possible in that sequence. This is the problem statement.
Okay. So is the problem statement clear to everyone? What the is problem statement saying? I hope that is clear.
Okay. Now the main point here is that how we will find should we find the whole sequence and the thing is it is just saying me that okay next number should be greater or smaller and it should be alternating. We don't know that what number we have to add and what we have to subtract. So what we will actually do is key let's say if we are starting from sequence S. Okay. So the next number as we told either it should be less than s either it should be greater than s up they because we want the maximum number. So we will always try to maximize the number of first and we also know that the maximum difference we can have between two number was m. So we will always try that the next number should be s + m because we will try to maximize but the problem is in the next number of this s plus m we can't make it s + 2m right that is what we will do exactly so if we can't make it s + 2m right so what we will do is we will make it s + m minus one because next number. If this is greater, this should be smaller. And because we want our numbers to be maximized to when we'll reduce the number, we reduce by one only. But when we will increase the number, we will increase always by how much maximum is possible. And now after this number should be greater. So now it should be s + 2 m minus one. Then s + 2 m minus 2.
So basically they go if your sequence is of size n if your sequence is is of size n to your first number is always s that no one can change. Now based on your n minus one n minus one numbers you have to decide how many times + one is happening and how many [clears throat] times minus one is happening that is what you will decide okay you go how it will happen to what I have done is first of all while solving the problem what I did I took this base case because because of this base case my answer got wrong okay so I got a wrong summation because of this base case I forgot the base case Because if n is one that means we want only one number in my sequence then answer will be simply s because s is the first element of the sequence. I will not do anything. Okay. But if n is not one then what I will do? I will decrease the value of n. Why we are decreasing the value of n? Because first number of the sequence is always s. We know that thing. The first number of the sequence is always s. So we will what we will do we will decrease n by one because we know that the first number to se now remaining n minus one numbers remaining n minus one numbers.
So I coded it during the contest in I think it was I just coded whatever I thought but now I'm just trying to rearrange the code. So now what we will do is we will say remaining n minus one numbers we will calculate see we will calculate also I will initially take my answer as zero. So initially I will take my answer as zero and because we know our first number will always be yes s always be s in my answer I will also add s. So is this much part clear to everyone what I just did? So we know that our first number will always be s. So I decrease n from my number because n minus one numbers are pending. Also initially I took my answer as zero. Then I added s in the answer. As simple as that. Now we need to calculate for the remaining n minus one numbers. Okay. Now see one thing guys. Now if remaining n minus one numbers are even. Let's say remaining n minus one numbers are 6. That means 3 * + m will happen and 3 * minus1 will happen. Is it right? And if that number is odd that means 7 * + 1 is m will happen and 3 * minus1 will happen because I will always start from plus n.
So if there are odd remaining numbers + one will happen one more time. Right?
Okay. So what I will do is I will write here no this code also I will try to optimize. So I will say plus m is happening how many times actually the uh the floor value uh uh the seal value of nx2. So I will say answer plus is equal to nx2 + n m into m because we know that this particular because we know if there are odd numbers then n /x2 + 1 will happen and there are even numbers simply nx2 will happen. So this is how we take the seal value of n /x2 into m. Okay. And how many times negative is happening? So negative is every time happening n by two times. If it is even then also half time. If it is odd then also half time. So I will subtract n by n by2 because minus one is happening n by two times. Now here comes one more base case.
If sequence is ending with minus one ml let's say this is four right and let's say I start from five 5 and this is 3 so it will become five + 3 8 -1 7 + 3 10 -1 9 So if we will include this minus1 also in case of even numbers Then answer will not be 9. It should be 10 because last minus one to 9 but 10 because this minus one is not considered. We don't have to find the last number of my sequence. We have to find the maximum of the sequence. So that is why if there is even n means ending with minus one are negative. So I will add one because this will be the maximum number.
Okay. And then we will return the answer. Okay. So I just tried to make it little more better. Uh I added some comments because previously what I solved was a little okay we got a wrong answer also here. Let's see what is the problem.
Uh okay.
Can anyone tell me?
Oh runtime error. Okay.
Do we need to find the answer in model?
No. Right.
Runtime error. Aa. Okay. Okay. So, because I'm multiplying here, I need to actually convert it to long long because answer will be very long. So, I better convert it to long long before multiplying. So, that is the problem.
Now, we did it. and uh submit.
So all these solutions I will say in the chat don't worry. So yeah it is done.
Okay. So this is how we are solving it.
Order the time complexity is order of one only. Space complex is order of one only. Everything most optimized.
Is this clear to everyone in the in the in the live? Does this make sense to all of you? Should we go towards the third problem of the contest?
Okay, how many problems have you guys solved by the way in your contest?
Solve so that I can understand if you have given the contest.
Great. So let's move forward to the third question.
So the third question is minimum adjacent swap to partition array. This is a very good problem and the logic of the problem is also really really good.
So let's understand I can't able to solve the third one but I have idea of DLGO.
Okay. Okay.
Understood. I will I will explain uh what this problem how we have to solve this problem. Let's first understand the problem. Okay. So the problem says that you are having an array. So you are having a array let's say 2 1 3 uh 5 7 2 1 6 okay let's say this is the problem and you are giving a number a and number b so let's say number a in our case is three number b is five okay what you have to do is all the numbers so you have to rearrange this array in such such a way that this array is divided into three parts.
First part of the array should contain numbers less than a. Second part of the array should contains number between a to b inclusive. Third part of the array should contain numbers greater than b. M after rearrangement your first part should contain 2 2 1 in any order. Order does not matter. A B part should contain three and five because that is only there and then it should contain six and six. Does this make sense? So you have to rearrange the array in such a way that you get a number like this.
Does this make sense to everyone?
Okay. Now the next thing to go we have this thing here. We have everything here rearrangement.
So rearrangement will also happen on the basis of swaps. So you can't do any rearrangement solve.
So how you can do these changes of the element? How you can make it a good array? By just swapping the adjacent element and you have to tell minimum number of times adjacent elements needs to be swapped.
Okay.
Let's understand how to solve this problem. Okay. So, is the problem statement clear to everyone?
Problem statement clear problem.
Let me take some water. I hope the problem statement is okay with the Ch great.
Okay, Kumar, you can check once. Okay, you can check the problem statement. It is there in the lead code. I have added the link in the description. Okay, so I will again repeat in a very quick recap.
We have an array and we have to rearrange the array in such a way that there are two numbers given to us A and B. We have to rearrange the array in such a way that key it the the array becomes with three different parts three different continuous part. First part should contains the element less than a last part should contains the element greater than b middle part should contain the elements between a to b inclusive also how you have to do this rearrangement by just swapping the adjacent elements. So means you can only swap two adjacent element. And the final answer is that you have to tell the minimum number of adjacent swaps required. Okay. Let's see how we can do it.
I'm for example if you will see we actually want key all these numbers less than three. There are four numbers less than three. 2 1 2 1.
Okay. So we need to actually move them to the first four position of the array.
Okay. Now tell me they go these two numbers are already at the first four position. Okay. So means we do not have to move these two to the first four position. That is very sure.
Okay. Means we need to move this number here. Okay.
Can anyone tell me how many swaps or rearrangements are required to move this particular number at the first four position? How many swaps are required?
Can anyone tell me guys?
Three, right? So the number of swaps required will be equivalent to number of elements before it which is greater than a. For example, they to the chart numbers 2 1 2 1. These numbers should be at the start of the array.
numbers to move two to here and to move one to here three three swaps each will be required is it right Number sense, right? The number of SHs are depend before a number. How many numbers are present which does not which should not be before that to three, five or six two and if they are present that means they need to be swapped.
Similarly, similarly for example five or three it is after this six once for example and this two should be at this position because it is less than a now let's move forward this is also one less than a this is also having no problem when we came to six so Six is a person uh six is a is a number which actually belong to partition three. So what I will do I will say partition 3 equal to 1 number partition present. Why I'm calculating it? I will tell you later. Okay. Now can anyone tell me which partition this three and five belong to?
they belong to partition two they up when we got three and three belongs to partition two but before three there was already a already a number of partition three I'm going logic is very clear first number is from partition one how we can define the partition using a and b to because the first number was less than I will say it is the part of partition one which is okay good qu I will go to the next number. Next number is also part of partition one. No problem. This number is part of partition three to take after partition one. Partition three may come but we will still have a count because count will be needed. I will tell you why. So I will say partition three equal to one number partition three.
So I will say partition three equal to 1.
Why this is helpful? Why we are doing this count? Because the next number that we are counting is of partition two.
Can a partition two number come after partition three number? No, it cannot come. So now I will check. This is a partition two number. But how many partition three numbers are present before that?
If this is a partition two element to how many elements of partition three are present before it element present partition we have counted it here. present answer one swap three or six I'm swap but we know it is important and now I will also count partition two element partition two element now when I will go to the next element this element is also of partition two and this also has a partition three element before that so this will also need a swap. So after three and six will be swap this will also need a swap right to answer a swap add because for both of the partition two elements we have a partition three element before that and now we have one partition three element two partition two elements.
Now coming on the third element up to go this was a partition one element.
Now tell me for this partition one element how many swaps are required for this two?
Can anyone tell me three swaps are required? Why? Because there are three numbers before them which of all partition two and partition three. How we will count? You can see this number is of partition one. But before this number there were two numbers coming from partition three and partition two. So this number has to skip all of them. So three swaps will be required. And same for this one also three swaps will be required. And because this 66 partition three number no problem. So here answer will be eight swaps.
So what I'm doing in the coding part for partition so actually this this is the count for partition two. This is the count for partition three. So when I'm getting a partition one element so I'm saying before partition one element how many partition two and partition three elements were there. Whatever elements were there that much straps are required I will add in the answer. Same when I get a partition two element to I'm increasing the count of partition two but in my answer I'm adding how many partition three elements were before and if we found a partition three element then nothing to do just increase their count. So with this logic we found out how many total swaps are required.
Does this make sense to everyone?
Edit Zed Kumar Chhattani.
Okay.
So this problem was good. It was order of n time complexity problem also little difficult. I would say it was not a easy problem. It was a medium level uh problem. Okay.
Third part will reach the end.
This problem is I would say a advanced version of sort color problem. Software problem of lead code is try. It is I would say advanced version of that problem.
Okay.
Should we move forward?
Take it. Let's move forward now. So, first time in your channel, sir. Thanks.
Thanks. Yeah. Thanks, Nanda Club.
Okay. Nice. Let's move forward now.
Getting on the last question. This question was really good. Uh it took me some time to solve it but yeah it was very good. Uh yeah rankings update.
There was a person at the first rank today and uh I saw that person I was thinking he must be doing some cheating also and yeah actually he was doing some cheating but let's come back.
So the problem is minimum cost to convert is string three. See this is the third problem of a problem. So this is a very famous problem of minimum cost to convert and uh two versions of the problems are already present in the uh you know u in lead code and this is the third version of this problem which we'll be solving today. Problem was very good. Let me first explain you this problem. It it will require some time to explain since it's a little longer problem. Okay. So, let me first explain the problem statement. Oh, no. Why is it?
So the problem says that you have four things with you. So these are the four inputs that are given to you. What are these inputs?
So they are giving you a target word, a source word, a 2D list of rules and a cost array. So four things are given to you. source and target string rules 2D array which contains the rule if you have to convert from one pattern to another pattern and the cost. So these things are given to you okay how you need to solve this problem. So what they want is that this is your source world and you need to convert this world to this target. Okay. Other question is how will you convert? How can you convert this hello to target? So to convert from hello to target you need to use this rules. Okay. And there are some constraints also. Constraints are that each rule can be used any number of times. So any number of times a rule can be used that is fine. Okay. But each character can be involved in one transaction only. So when you are converting from source to target let's say convert you can't use that a again. So one index can be involved in only one rule conversion but same rule can be used in multiple places. So ultimately you have to find that what will be the minimum cost of conversion. For example, this is a very very easy test case they have given. Obviously, they will give easy only. So according to this test case, for these first two characters, you will use the rule one which will convert it to WO. So H E to WO is the rule and cost is three and then L2 R L cost is four. The final cost is seven and the value is 1.
Do you understand this?
Okay. So this is how we are getting seven and this is the one.
Okay. Now the next point. So this is a very straightforward thing. Let's take a little little you know on a hard question to let's take a question where I have a word called abina this is the source word okay and I have the word called target word is aasti So, so the thing is that you have your source word as a and target word as a Okay.
So the question is that uh the question is minimum cost to convert a string three and let me explain you the problem statement here. It's a very long problem statement. Let me explain you what is there in the problem. So let's say your source string given to you is Abina, target string is Austri. Okay. Now there are some conversion rules given to you with which you can actually convert from one source to another. So rules are like this. Let's say that there are four rules given to you. I will try to make a problem. I will try to make a example which can fit in all the use cases.
Okay. So let's say first rule is so every rule will have a pattern and a replacement and a cost for that conversion from that pattern that replacement. So let's say first one is that replace A B to A with a cost of so this is the cost I'm writing here with a cost of let's say seven okay so are you guys following me please write a plus one if you guys are following okay since it's a little difficult problem so I will take some time to explain give me 20 20 minutes 15 20 minutes you'll understandable. Okay. And I hope you guys are following me. Okay. So this second we can maybe have a A B2 A W with a cost of two.
F with a cost of four.
Then we can have uh uh H I N to A st here now you can have some wild characters also. So this means that in this place of this anything can happen.
So it will be a st let's say with a cost of three. Yeah for five then we can have a v to let's say h i. There can be some rules also which are not right which are not actually converting to the right uh target but I'm not taking those rules. Those rules will be definitely be rejected. Let's say I can make a rule of ABC to XY Z.
Obviously ABC to XY Z rule will never be considered because there is no match of source no match of target. I'm not even considering those rules. Okay. So this will be let's say three or n hi with rule of four. Okay. So let's say this is what we are doing. Now up in this question the pro the thing is which rule we can have up we can have combination of multiple rules we can have. So let's say this is the rule number first, second, third, fourth, fifth. So you can also take combination of rule first and rule fifth. It will also give you the answer with the cost of 7 + 4 because this is 7, this is four. Or you can take the rule of uh let's say second ab + 3 + 4th which is 4 + 5 + 3 and because it has a wild character also so they are saying for wild card you need to add plus one more. So here cost was 11 here cost is 13.
The thing is we need to find the minimum cost which is 11 in our case. But how to find? Whenever you have multiple cases, multiple solutions are possible, we always try to go with the DP.
So here also I will try to go with the DP approach. So what we will do? We will create a DP. So here you will see the size of source is seven.
Okay. So because the size is seven, I will take a DP of size seven or actually I will take DP of size eight so that I can have indexes up to seven.
So seven this is the DP initially I will keep them as very infinity because we don't know the minimum cost. DP of 0 comma 0 I will keep at zero. So this is so here DP of I + 1 here DP of I + 1 will tell you that what is the minimum cost of uh yeah yes DP of I plus will tell what will the minimum cost to convert source to target till index number I from zero. Okay. So what we will do here I will make DP of zero as zero because zero means no characters so cost is zero. Now one by one I will check to in the question also what I have done. So I took this N M I took the DP make 0 to zero. Now I will simply start with the zero index of the string.
Now for each index I see what I will do.
Okay, they can be if DP of I let's say we are checking for index number we are checking from this index but we even don't know how to convert AB H to A WA if we have not found the way for that we will not even check for starting from I because then you have to check for a longer will so I will not check this but what I will do if my source I and target I is already equal to then my number of operations will be zero. That will be one case. So there's a for example okay there is no such case but let's say there's a case to because the first character is a here and in the target also it's a to when we are at this index so 0 1 2 3 4 5 6 0 1 2 3 4 5 6 this is a and this is also a I will say that till index number one till index number zero answer is zero because 0 and 0 already equal no oper operations are required.
No rule is required. Okay. But still I will try to find some other pattern starting from A. So what I will do? I will now go through all the patterns. So I am iterate through all the rules. Okay. So what I will do?
I will iterate through all the rules of that string. Now for each rule what we will do? So for each rule I will first get the pattern and replacement. For example, I will get the first rule from AB to Austri.
I will first of all find that whether this rule even exists or not. How will I find? So because this rule uh in this rule pattern length is four. So I will go till the four length of pattern. I will see whether pattern and the target and replacement is matching or not. I will check that. So I will check if my pattern is either star because star matches with anything or if pattern index is matching the source and replacement is matching matching the target means if this is here. So you can see match and this pa ai is match.
So what I will do is because they both are matching this aasti and this aasti.
So I will say yes this rule is possible.
So this rule is at index number three month fourth index I will say the the cost of this rule is seven I will say four is seven z but that is not the only rule that is applicable here I will also say that to seven it will see is the second rule possible yes second rule be possible here and second rule a convert with the cost of four to we I will also write secondly four cost because you can see I'm taking every time the minimum cost so cost of the rule is cost plus while character to skip it okay after that none of these rules will be applicable starting from this A so I will move forward to B and W for B also there is no rule applicable then I will go to H for H there is a rule applicable by the way here this question can't be optimized so we will see only those rule which are applicable but it is fine the time complexity of this was actually N into R into L where n is number length of this string r is number of rules and length is the length of the pattern which is working for lead code okay so meanwhile h let's come back to the question so what we are thing is we will say that this is H up. We have to find a rule which is matching to this pattern starting from H. And you can see this rule is matching H star and is matching to H in N because this is a wild card entry and here it is A also means this rule will work ending at which index?
Fourth index. So here DP is + one. So here five will become cost which was four plus is conversion cost which was 5 + 1 6 because there is a wild card entry. That is what we are doing. We are adding DP of I plus this cost rule which is cost plus count of wild card. This is count of wild card which is 5 + 1 6 and then 5 + 1 6 and then uh + 4 10 Next to 13 but 11 is already minimum the final answer will be 11. So what we are doing after checking for all the patterns I'm finding key after considering that pattern first of all we will see if that pattern is possible. So if that pattern is possible to take from that index of the string I index to whichever at whatever index that pattern is ending.
before cost. So we will update the cost and you'll find the DP. So this was a hard problem. No doubt it was a hard problem. Okay. Uh you need to understand once the code my code link I will share in the description. You have to go through the code again. You may have to watch the the so okay this is the question number four. By the way I think uh I have wrongly mentioned here. I forgot to update. So this was the question number four that we were discussing actually to discuss but to uh you have to go through once uh this question. Okay, this is a little so this was a kind of brute force only app uh brute force plus DP uh time complex stage. This is order of n then this one was order of r which is number of rules and this is order of l which is the length of the pattern. So it should be nl.
So yeah that is this problem.
So guys, these were the four problems of this contest which is uh bi-weekly contest 187.
I hope they are clear to everyone.
Fourth problem was little more difficult. I agree. So that you guys have to uh try once more. Try to see the code. Try to check the video again. Then you will understand for sure you have to invest some time. Even I invested I think more than half an hour on this last problem. So you have to invest some time on that.
Okay, cool guys. So, is this clear everyone?
Any doubts? Does anyone has all good?
Okay. Cool. Then guys, uh thanks for joining the session. We will uh conclude it now. Um, all the very best for your next contest. We'll meet again with a new video. I will try to be regular. Till then, thank you guys. Take care. Bye-bye. Good night.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

2.4 BILLION Records Got Leaked...
DeepHumor
15K views•2026-07-22

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Should I buy a Sawmill?
essentialcraftsman
29K views•2026-07-22

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23