Hi, everyone, welcome back.
So in this video, we are going to write the code for the alarm and as discussed, what we need, we
need one map and we need one list, list of keys that list alkies firmly attached to the oldest late.
So let's start writing the code.
We will need one map.
So let's create one map map of integer integer developing.
Right.
And let's say the name is my map and we need one more thing, which is list.
Right.
List of integers to restore Restore Our Keys.
So let's say the name is what should be the name.
Let's say the name is a list list.
Right.
And we should also have element elements, all basically capacity.
So let's rename it to capacity only.
Right.
So in the constructor, what do I need to do, I will update the capacity rate, so capacity is the
input one.
So I want to refer to this capacity.
All right.
Let's change the name.
So let's name it to S.P. capacity.
So what recipe is the given capacity?
And let's do one more thing, let's get our map, if they are starting any default value or any garbage
value, let's clear them here in the constructor.
So my map not clear and list darte clear.
Fine.
Now let's talk about the good function.
So given a key, I want to return its corresponding value, right?
So what I will do, I will check and map.
So my map d'arte find check whether this key is present or not.
So if this key is not present.
So if this key is not present, then in that case I'm going to return minus one, that the key is not
present.
Now in the ls part, the key is actually present.
So if the keys present, can I write like this.
Return my map of key.
Can I write like this.
No, this is wrong.
What we need to do, we discussed that if the key we discussed that this key is basically the latest
element analysis is the latest element, we will remove the element from the list and we will put the
element in the beginning.
Right.
So we need to do two other things.
And what are those two things?
So we need to remove the element.
Right.
So let's remove from list the given key, because this is the latest element.
So we need to push we need to insert this element at front.
Right, because this is the latest one.
So I'm inserting it friend now.
It's correct.
Now, let's talk about this put function.
Right.
So first of all, let me check whether this key exists or not.
So my map doesn't find the given key.
Is it close to my map dart?
And so I am checking whether the key is present in map or not.
Right.
So if the given is not present in the map, then I will do something in the ls part.
This is basically a new key value pair.
Sorry, not new.
The key already exists in the map.
The value is new since the value is new.
What we need to do, we need to update our map.
Right.
So let's update our map.
Let's update the value for the given key.
So the new value for the given key is this value.
So what I'm doing here is see what can happen.
Let's say your map is containing.
This key will appear one, two.
And now I have one put operation one pot operation, and that operation is one comadre.
So basically, if the key is already present, if the key is already present, then you need to update
the value to be three.
And I'm doing that only.
Right.
But this is not enough since this key is the latest element.
Now, this is the latest element now.
So what you need to do again, exactly what we have done above here, we need to remove the key and
we need to push that, giving it to insert the key in the front of the list, because this is the latest
element.
Now.
Right.
So we need to do these two things also.
Now, if the given key is not present, let's talk about this case.
The given key is not present.
We have a new key, let's say the key, which I want to introduce to come forward, which is not present
in the map.
Right.
The key is not present.
This key is coming for the first time.
So what we will do so first of all, we need to check whether whether the list is full or not made.
It may be possible.
The list is already full.
So how to check?
We can check like this.
So if the list that says.
Is it close to the capacity of a location that means full, right, we are full.
I cannot take any that element right so far inserting this particular key.
But we have to do we need to remove the last element, the oldest element.
And that is the last element.
Last element will be present at the last.
So this bag function, but this back function will return.
This function will return the last value.
Right.
This back function will give you the last value.
I want to get the last value because we need to delete this value.
So for deleting the value, what you can do and let's start back.
Remove the lost value, and we also need to remove this value from the map, so my map that it is and
it will take as input.
And what is the key?
Key is basically last night.
So before doing Bombeck the back and this BOBETTE function, this will remove the last element.
So before removing the last element, we need to store that last element so that we can erase that developer
from the map also.
So that's why we need to write this line before this line.
You got my point.
What I'm trying to say here is my map.
My location is already for you and I want to insert this new element.
So the only option is I have to delete the oldest element and the oldest element is present at last.
So by this line, you are removing the oldest element.
But we also need to remove the developer from the map.
So first you need to store that value and then you can remove from the list and then you can remove
from the map.
Also, that is the way to do this.
So this is the work that we need to do.
This is the work that we need to do when the list is full, right?
If the list is not full, then we'll simply insert a list and simply insert in map.
So software inserting in map.
My map of key.
It calls today, it goes to this value and we need to insert a list also and this is the latest element,
so very will insert you will insert add friend.
So let's start Bush friend.
And this is key made simple.
So I think everything is fine.
Now let's try to reach our goal.
And if everything works, then we will submit.
OK, we got some compilation error.
OK, so this will be end.
Yep, our code is working for the basic best cases.
Let's go now.
So basically, our goal is working absolutely fine, right?
So these are very simple, simple function.
I told you we need a map and we need a list.
Why do we need a list?
Because we need one data structure so that I can I can access different element as well as the back
element.
Right.
Why do I need to access back element so that I can remove the oldest element?
And why do I need to access for element?
Because we need to store the latest element at front rate.
So that's why we need to support both store that support boat operation that support both front and
back, that has access to both the elements front and back in constant pain.
So this list supports over time insertion and deletion in front as well as in the end date.
So what I'm trying to say it is this list, support over time, operation on different element, whether
you are inserting or deleting different element and also whether you are inserting or deleting the last
element.
Right.
So that's why we need this data and not simple at simple vector that will not work.
And this is our normal map.
And we already discussed great function but function.
And this is a very simple constructor that should guys, that is all that I want to cover in this video.
I will share the call with you and I will meet you in the next one.
Thank you.