1 00:00:01,170 --> 00:00:02,640 Hi, everyone, welcome back. 2 00:00:02,700 --> 00:00:09,300 So in this video, we are going to write the code for the alarm and as discussed, what we need, we 3 00:00:09,300 --> 00:00:16,830 need one map and we need one list, list of keys that list alkies firmly attached to the oldest late. 4 00:00:17,100 --> 00:00:18,780 So let's start writing the code. 5 00:00:19,200 --> 00:00:20,550 We will need one map. 6 00:00:20,850 --> 00:00:24,920 So let's create one map map of integer integer developing. 7 00:00:24,950 --> 00:00:25,350 Right. 8 00:00:25,710 --> 00:00:33,210 And let's say the name is my map and we need one more thing, which is list. 9 00:00:33,210 --> 00:00:33,630 Right. 10 00:00:34,110 --> 00:00:37,220 List of integers to restore Restore Our Keys. 11 00:00:37,950 --> 00:00:41,870 So let's say the name is what should be the name. 12 00:00:41,880 --> 00:00:45,540 Let's say the name is a list list. 13 00:00:45,720 --> 00:00:46,140 Right. 14 00:00:46,440 --> 00:00:53,640 And we should also have element elements, all basically capacity. 15 00:00:53,640 --> 00:00:56,280 So let's rename it to capacity only. 16 00:00:58,660 --> 00:00:59,030 Right. 17 00:00:59,560 --> 00:01:07,130 So in the constructor, what do I need to do, I will update the capacity rate, so capacity is the 18 00:01:07,150 --> 00:01:07,810 input one. 19 00:01:07,810 --> 00:01:11,240 So I want to refer to this capacity. 20 00:01:11,260 --> 00:01:11,570 All right. 21 00:01:11,650 --> 00:01:12,580 Let's change the name. 22 00:01:12,590 --> 00:01:15,810 So let's name it to S.P. capacity. 23 00:01:17,260 --> 00:01:20,950 So what recipe is the given capacity? 24 00:01:23,890 --> 00:01:30,730 And let's do one more thing, let's get our map, if they are starting any default value or any garbage 25 00:01:30,730 --> 00:01:33,290 value, let's clear them here in the constructor. 26 00:01:33,730 --> 00:01:37,060 So my map not clear and list darte clear. 27 00:01:39,550 --> 00:01:39,970 Fine. 28 00:01:41,740 --> 00:01:44,000 Now let's talk about the good function. 29 00:01:44,500 --> 00:01:50,150 So given a key, I want to return its corresponding value, right? 30 00:01:50,410 --> 00:01:52,260 So what I will do, I will check and map. 31 00:01:52,570 --> 00:01:57,880 So my map d'arte find check whether this key is present or not. 32 00:01:58,720 --> 00:02:03,700 So if this key is not present. 33 00:02:05,380 --> 00:02:13,060 So if this key is not present, then in that case I'm going to return minus one, that the key is not 34 00:02:13,060 --> 00:02:13,530 present. 35 00:02:14,290 --> 00:02:17,630 Now in the ls part, the key is actually present. 36 00:02:17,650 --> 00:02:20,380 So if the keys present, can I write like this. 37 00:02:20,380 --> 00:02:23,260 Return my map of key. 38 00:02:24,790 --> 00:02:25,970 Can I write like this. 39 00:02:26,920 --> 00:02:28,850 No, this is wrong. 40 00:02:29,350 --> 00:02:37,450 What we need to do, we discussed that if the key we discussed that this key is basically the latest 41 00:02:37,450 --> 00:02:44,590 element analysis is the latest element, we will remove the element from the list and we will put the 42 00:02:44,590 --> 00:02:45,970 element in the beginning. 43 00:02:46,360 --> 00:02:46,730 Right. 44 00:02:47,080 --> 00:02:49,050 So we need to do two other things. 45 00:02:50,230 --> 00:02:51,740 And what are those two things? 46 00:02:52,120 --> 00:02:54,470 So we need to remove the element. 47 00:02:54,570 --> 00:02:55,000 Right. 48 00:02:55,300 --> 00:03:01,320 So let's remove from list the given key, because this is the latest element. 49 00:03:01,330 --> 00:03:05,950 So we need to push we need to insert this element at front. 50 00:03:06,160 --> 00:03:08,220 Right, because this is the latest one. 51 00:03:08,650 --> 00:03:12,160 So I'm inserting it friend now. 52 00:03:12,160 --> 00:03:12,760 It's correct. 53 00:03:13,960 --> 00:03:16,540 Now, let's talk about this put function. 54 00:03:17,800 --> 00:03:18,210 Right. 55 00:03:18,670 --> 00:03:23,200 So first of all, let me check whether this key exists or not. 56 00:03:23,620 --> 00:03:29,740 So my map doesn't find the given key. 57 00:03:29,950 --> 00:03:32,800 Is it close to my map dart? 58 00:03:32,950 --> 00:03:37,030 And so I am checking whether the key is present in map or not. 59 00:03:37,390 --> 00:03:37,820 Right. 60 00:03:38,020 --> 00:03:42,600 So if the given is not present in the map, then I will do something in the ls part. 61 00:03:43,450 --> 00:03:45,640 This is basically a new key value pair. 62 00:03:46,660 --> 00:03:47,610 Sorry, not new. 63 00:03:47,860 --> 00:03:49,790 The key already exists in the map. 64 00:03:49,810 --> 00:03:52,470 The value is new since the value is new. 65 00:03:52,630 --> 00:03:55,480 What we need to do, we need to update our map. 66 00:03:55,480 --> 00:03:55,860 Right. 67 00:03:56,140 --> 00:03:57,700 So let's update our map. 68 00:03:59,510 --> 00:04:01,830 Let's update the value for the given key. 69 00:04:01,840 --> 00:04:04,510 So the new value for the given key is this value. 70 00:04:05,020 --> 00:04:07,890 So what I'm doing here is see what can happen. 71 00:04:08,170 --> 00:04:09,880 Let's say your map is containing. 72 00:04:10,060 --> 00:04:11,860 This key will appear one, two. 73 00:04:12,190 --> 00:04:17,730 And now I have one put operation one pot operation, and that operation is one comadre. 74 00:04:18,010 --> 00:04:23,440 So basically, if the key is already present, if the key is already present, then you need to update 75 00:04:23,440 --> 00:04:24,520 the value to be three. 76 00:04:24,550 --> 00:04:25,960 And I'm doing that only. 77 00:04:26,200 --> 00:04:26,590 Right. 78 00:04:26,920 --> 00:04:31,720 But this is not enough since this key is the latest element. 79 00:04:31,750 --> 00:04:33,850 Now, this is the latest element now. 80 00:04:33,850 --> 00:04:40,900 So what you need to do again, exactly what we have done above here, we need to remove the key and 81 00:04:40,900 --> 00:04:48,850 we need to push that, giving it to insert the key in the front of the list, because this is the latest 82 00:04:48,850 --> 00:04:49,340 element. 83 00:04:49,410 --> 00:04:49,680 Now. 84 00:04:49,890 --> 00:04:50,310 Right. 85 00:04:50,470 --> 00:04:52,750 So we need to do these two things also. 86 00:04:52,780 --> 00:04:56,020 Now, if the given key is not present, let's talk about this case. 87 00:04:56,410 --> 00:04:58,660 The given key is not present. 88 00:04:59,140 --> 00:05:05,500 We have a new key, let's say the key, which I want to introduce to come forward, which is not present 89 00:05:05,500 --> 00:05:06,130 in the map. 90 00:05:06,310 --> 00:05:06,680 Right. 91 00:05:06,910 --> 00:05:08,100 The key is not present. 92 00:05:08,140 --> 00:05:09,650 This key is coming for the first time. 93 00:05:09,850 --> 00:05:16,180 So what we will do so first of all, we need to check whether whether the list is full or not made. 94 00:05:16,390 --> 00:05:17,320 It may be possible. 95 00:05:17,320 --> 00:05:18,500 The list is already full. 96 00:05:19,060 --> 00:05:20,080 So how to check? 97 00:05:20,080 --> 00:05:21,300 We can check like this. 98 00:05:21,580 --> 00:05:23,890 So if the list that says. 99 00:05:25,100 --> 00:05:32,070 Is it close to the capacity of a location that means full, right, we are full. 100 00:05:32,570 --> 00:05:38,450 I cannot take any that element right so far inserting this particular key. 101 00:05:38,450 --> 00:05:42,800 But we have to do we need to remove the last element, the oldest element. 102 00:05:43,250 --> 00:05:44,780 And that is the last element. 103 00:05:44,780 --> 00:05:47,570 Last element will be present at the last. 104 00:05:48,170 --> 00:05:52,640 So this bag function, but this back function will return. 105 00:05:53,090 --> 00:05:55,340 This function will return the last value. 106 00:05:56,060 --> 00:05:56,450 Right. 107 00:05:56,690 --> 00:05:59,390 This back function will give you the last value. 108 00:06:00,140 --> 00:06:03,670 I want to get the last value because we need to delete this value. 109 00:06:04,340 --> 00:06:08,690 So for deleting the value, what you can do and let's start back. 110 00:06:11,450 --> 00:06:20,210 Remove the lost value, and we also need to remove this value from the map, so my map that it is and 111 00:06:20,210 --> 00:06:22,100 it will take as input. 112 00:06:22,100 --> 00:06:22,970 And what is the key? 113 00:06:23,260 --> 00:06:26,620 Key is basically last night. 114 00:06:26,870 --> 00:06:33,030 So before doing Bombeck the back and this BOBETTE function, this will remove the last element. 115 00:06:33,050 --> 00:06:39,230 So before removing the last element, we need to store that last element so that we can erase that developer 116 00:06:39,230 --> 00:06:40,140 from the map also. 117 00:06:40,160 --> 00:06:43,570 So that's why we need to write this line before this line. 118 00:06:43,880 --> 00:06:44,920 You got my point. 119 00:06:45,410 --> 00:06:47,810 What I'm trying to say here is my map. 120 00:06:48,020 --> 00:06:51,940 My location is already for you and I want to insert this new element. 121 00:06:52,070 --> 00:06:57,140 So the only option is I have to delete the oldest element and the oldest element is present at last. 122 00:06:57,380 --> 00:07:00,170 So by this line, you are removing the oldest element. 123 00:07:00,170 --> 00:07:03,330 But we also need to remove the developer from the map. 124 00:07:03,620 --> 00:07:09,350 So first you need to store that value and then you can remove from the list and then you can remove 125 00:07:09,350 --> 00:07:09,980 from the map. 126 00:07:09,980 --> 00:07:12,710 Also, that is the way to do this. 127 00:07:13,140 --> 00:07:15,310 So this is the work that we need to do. 128 00:07:17,840 --> 00:07:21,910 This is the work that we need to do when the list is full, right? 129 00:07:21,950 --> 00:07:26,690 If the list is not full, then we'll simply insert a list and simply insert in map. 130 00:07:27,160 --> 00:07:28,580 So software inserting in map. 131 00:07:32,070 --> 00:07:33,420 My map of key. 132 00:07:37,160 --> 00:07:43,880 It calls today, it goes to this value and we need to insert a list also and this is the latest element, 133 00:07:43,880 --> 00:07:47,280 so very will insert you will insert add friend. 134 00:07:47,390 --> 00:07:49,280 So let's start Bush friend. 135 00:07:49,280 --> 00:07:54,220 And this is key made simple. 136 00:07:54,800 --> 00:07:57,430 So I think everything is fine. 137 00:07:57,440 --> 00:07:58,940 Now let's try to reach our goal. 138 00:07:58,970 --> 00:08:01,960 And if everything works, then we will submit. 139 00:08:03,110 --> 00:08:05,310 OK, we got some compilation error. 140 00:08:07,080 --> 00:08:08,960 OK, so this will be end. 141 00:08:19,250 --> 00:08:22,350 Yep, our code is working for the basic best cases. 142 00:08:22,370 --> 00:08:23,630 Let's go now. 143 00:08:28,430 --> 00:08:32,419 So basically, our goal is working absolutely fine, right? 144 00:08:32,450 --> 00:08:34,350 So these are very simple, simple function. 145 00:08:34,370 --> 00:08:37,580 I told you we need a map and we need a list. 146 00:08:38,330 --> 00:08:39,440 Why do we need a list? 147 00:08:39,799 --> 00:08:45,890 Because we need one data structure so that I can I can access different element as well as the back 148 00:08:45,890 --> 00:08:46,390 element. 149 00:08:46,400 --> 00:08:46,730 Right. 150 00:08:47,210 --> 00:08:51,260 Why do I need to access back element so that I can remove the oldest element? 151 00:08:51,710 --> 00:08:54,220 And why do I need to access for element? 152 00:08:54,260 --> 00:08:57,550 Because we need to store the latest element at front rate. 153 00:08:57,680 --> 00:09:05,840 So that's why we need to support both store that support boat operation that support both front and 154 00:09:05,840 --> 00:09:10,430 back, that has access to both the elements front and back in constant pain. 155 00:09:11,690 --> 00:09:20,090 So this list supports over time insertion and deletion in front as well as in the end date. 156 00:09:20,110 --> 00:09:29,420 So what I'm trying to say it is this list, support over time, operation on different element, whether 157 00:09:29,420 --> 00:09:34,670 you are inserting or deleting different element and also whether you are inserting or deleting the last 158 00:09:34,670 --> 00:09:35,140 element. 159 00:09:35,720 --> 00:09:36,160 Right. 160 00:09:36,200 --> 00:09:41,180 So that's why we need this data and not simple at simple vector that will not work. 161 00:09:41,870 --> 00:09:43,790 And this is our normal map. 162 00:09:43,790 --> 00:09:46,520 And we already discussed great function but function. 163 00:09:46,520 --> 00:09:52,850 And this is a very simple constructor that should guys, that is all that I want to cover in this video. 164 00:09:53,390 --> 00:09:56,330 I will share the call with you and I will meet you in the next one. 165 00:09:56,360 --> 00:09:56,960 Thank you.