LRU Cache | Brute Force | Optimal | Detailed | Leetcode 146

  Рет қаралды 15,810

codestorywithMIK

codestorywithMIK

Күн бұрын

This is the 2nd Video on our Design Data Structure Playlist.
In this video we will try to solve a very good and famous and interesting Problem "LRU Cache" (Leetcode 146)
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : LRU Cache
Company Tags : Adobe, Microsoft, Amazon, Citygroup, Twitch and many more shown in video
My solutions on Github : github.com/MAZ...
Leetcode Link : leetcode.com/p...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips
#interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook

Пікірлер: 167
@bhartipatel7833
@bhartipatel7833 Жыл бұрын
Plz keep uploading such videos It really helps 🙏
@souradeep.23
@souradeep.23 Жыл бұрын
true 🔥
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Guys, let’s do LRU Cache first. This will help us to solve LFU Cache Next video will be on LFU Cache Thank you all for watching ❤❤❤
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Aaay Sir 🔥
@AbhijeetKumarAJ
@AbhijeetKumarAJ Жыл бұрын
yeh pre-requisite hai for LFU cache?
@niteshk0487
@niteshk0487 Жыл бұрын
@@AbhijeetKumarAJ No bro pre-requisite is LRU
@AbhijeetKumarAJ
@AbhijeetKumarAJ Жыл бұрын
@@niteshk0487 I AM ASKING is LRU pre-requisite for LFU
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yea this qn should be solved before LFU It will make it easy for all of us to understand LFU Cache
@singerpranavmodi
@singerpranavmodi 11 ай бұрын
I hope I would have got this channel before. You are really awesome bro!!!
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
It means a lot to me. Thank you so much 😇🙏
@vineetkumarmotwani474
@vineetkumarmotwani474 Жыл бұрын
Went through the comments and disappointed to see that not even a single soul came up with the queue solution. Anyways, here is my implementation for the same: class LRUCache { int size; queue q; map mp, cnt; public: LRUCache(int capacity) { size = capacity; mp.clear(); } int get(int key) { if (cnt.find(key) == cnt.end()) return -1; q.push(key); cnt[key]++; if(mp.find(key) != mp.end()) return mp[key]; return -1; } void put(int key, int value) { q.push(key); cnt[key]++; mp[key] = value; while (cnt.size() > size) { int cur = q.front(); q.pop(); if (cnt[cur]-- == 1) cnt.erase(cur); } } }; /** * Your LRUCache object will be instantiated and called as such: * LRUCache* obj = new LRUCache(capacity); * int param_1 = obj->get(key); * obj->put(key,value); */
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much for sharing ❤️❤️
@AnandKumar-kz3ls
@AnandKumar-kz3ls Жыл бұрын
Brilliant explanation as always Instead of adding to the front you can also add to the back only problem is dll.end() return the iterator just after last element we need to subtract the iterator by 1 example: auto itr=dll.end(); itr--; map[key].first=itr; i think erase time complexity of unorderded map is O(N) in worst case which can be avoided by assigning dll.end() and just need to add one check if(map.find(key)!=map.end() && map[key].first!=dll.end() ); and should use unordered_map instead of ordered map
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed . Feels great you mentioned this one ❤️❤️❤️
@nikhil_1852
@nikhil_1852 10 күн бұрын
i know how to solve this problem but badee din dimag mai confusion tha ki bhai singly linked list bhi use kr sakta tha mai ..now i got know why i used dll here
@AbhijeetKumarAJ
@AbhijeetKumarAJ Жыл бұрын
so the optimal solution time complexity is O(1)? and space complexity is O(n) as we are storing data in doubly linked list?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Absolutely correct Abhijeet ❤️
@meoww-c3b
@meoww-c3b Ай бұрын
Java Solution using DLL and map to store Key and address created addNode and delNode function class LRUCache { class Node{ int key; int val; Node prev; Node next; Node(int key, int val){ this.key = key; this.val = val; } } Node head = new Node(-1, -1); Node tail = new Node(-1, -1); int cap; HashMap m = new HashMap(); public LRUCache(int capacity) { cap = capacity; head.next = tail; tail.prev = head; } // function to add a new node private void addNode(Node newnode){ Node temp = head.next; newnode.next = temp; newnode.prev = head; head.next = newnode; temp.prev = newnode; } // function to delete a node private void deleteNode(Node delnode){ Node prevv = delnode.prev; Node nextt = delnode.next; // join previous and next to remove node prevv.next = nextt; nextt.prev = prevv; } public int get(int key) { // if present in map if(m.containsKey(key)){ Node resnode = m.get(key); // stores value int ans = resnode.val; // makes it most frequently used // remove from map m.remove(key); // remove from node deleteNode(resnode); // add to node addNode(resnode); // add to map m.put(key,head.next); return ans; } // is not present in map and LL return -1; } public void put(int key, int value) { // if it already contains key so we can later readd if (m.containsKey(key)){ Node curr = m.get(key); m.remove(key); deleteNode(curr); } // if it is of filled size if (m.size() == cap){ m.remove(tail.prev.key); deleteNode(tail.prev); } // simply add it at head addNode(new Node(key, value)); // put key and new address in map m.put(key, head.next); } }
@vladimirprotein9693
@vladimirprotein9693 Ай бұрын
Thanks brother!!
@closer9689
@closer9689 Ай бұрын
Awesome👏👏👏👏....................I was using a Deque😦
@sreejasree7427
@sreejasree7427 Ай бұрын
Thankyou sir , best explanation
@hameedmulani21
@hameedmulani21 Жыл бұрын
Mind Blowing
@nagmakhan672
@nagmakhan672 Жыл бұрын
Amazing. Thanks a lot Need java code if possible
@Auto_Enthusiast
@Auto_Enthusiast Жыл бұрын
awesome explanation , bhai appke smjhane ka kya hi jawaab hai , mtlb shbd km pd jayenge!!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot 😇🙏
@invinciblearmyvlogsshorts7911
@invinciblearmyvlogsshorts7911 Жыл бұрын
bhaiya , i don't know , but the way you build intution in a problem is something which make you unique....pls make videos on recursion and DP.. we will always support you
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Sure thing
@kunal4710
@kunal4710 Жыл бұрын
BHAIYA ONE DOUBT IF THIS QUESTION WILL BE ASKED IN A INTERVIEW AND THE INTERVIEWER DIDNT ALLOW US TO USE STL LIST THEN WHAT WILL WE DO I DONT THINK I CAN MAKE MY OWN DOUBLY LINKED LIST CLASSS AND TGEN EVERYTIME CHANGE LINKS FOR EVERY OPERATION?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Kunal, It actually depends. There can be many scenarios : 1) If you are a fresher then they will expect you to know about Data Structure and would want you to implement dll 2) Since this qn is intself a lengthy, and implementing dll separate might take a lot of time because of which interviewer might not be interested in the dll implementation 3) Interviewer might only ask how will you implement dll and then let you continue with stl . 4) When I had cracked Microsoft, there was a qn which was short and it required min heap. Since the qn was short, i was asked to implement min heap on my own.
@kunal4710
@kunal4710 Жыл бұрын
@@codestorywithMIK Got it bhaiya thanks for the reply 🤩
@MounikaEMB
@MounikaEMB Жыл бұрын
I have a confusion, why we need to remove [2,2] in the beginning, as per algo we need to remove [1,1] which is added before [2,2].. Can you explain it clearly??
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Because [1,1] was just recently used. Hence it’s fresh. [2,2] was not recently used that’s why it’s not fresh. NOTE : Qn does not say to remove those which added before, qn is saying to remove those which was LEAST recently used . See 3:33 of this video. Hope that will help 😇🙏❤️
@abhishekkarn8918
@abhishekkarn8918 6 ай бұрын
Amazing explanation.Way to go brother.
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Thanks, bhai. Below are both the Java solution: Brute Force: class LRUCache { List cache; int n; public LRUCache(int capacity) { n = capacity; cache = new ArrayList(); } public int get(int key) { for(int i = 0 ; i < cache.size(); i++){ if(cache.get(i).key == key){ int val = cache.get(i).value; Pair temp = new Pair(key, val); cache.remove(i); cache.add(temp); return val; } } return -1; } public void put(int key, int value) { for(int i = 0 ; i < cache.size(); i++){ if(cache.get(i).key == key){ cache.remove(i); cache.add(new Pair(key, value)); return; } } if(cache.size() == n){ cache.remove(0); cache.add(new Pair(key, value)); } else { cache.add(new Pair(key, value)); } } } class Pair { int key; int value; Pair(int _key, int _value){ this.key = _key; this.value = _value; } } Optimal: class LRUCache { Node head = new Node(0, 0), tail = new Node(0, 0); Map < Integer, Node > map = new HashMap(); int capacity; public LRUCache(int _capacity) { capacity = _capacity; head.next = tail; tail.prev = head; } public int get(int key) { if (map.containsKey(key)) { Node node = map.get(key); remove(node); insert(node); return node.value; } return -1; } public void put(int key, int value) { if (map.containsKey(key)) { remove(map.get(key)); } if (map.size() == capacity) { remove(tail.prev); } insert(new Node(key, value)); } private void remove(Node node) { map.remove(node.key); node.prev.next = node.next; node.next.prev = node.prev; } private void insert(Node node) { map.put(node.key, node); Node headNext = head.next; head.next = node; node.prev = head; headNext.prev = node; node.next = headNext; } class Node { Node prev, next; int key, value; Node(int _key, int _value) { key = _key; value = _value; } } }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
You are a Life saviour.
@nagmakhan672
@nagmakhan672 Жыл бұрын
Wow. tysm
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
thank you bhai and Nagma
@nit8826
@nit8826 Жыл бұрын
Sir,Issi ke sath LFU ka bhi dekh lete toh kaam ho jata
@codestorywithMIK
@codestorywithMIK Жыл бұрын
LFU video uploaded in this playlist 🙏😇❤️
@shikharpandya4927
@shikharpandya4927 2 ай бұрын
Thanks a lot sir
@gui-codes
@gui-codes 3 күн бұрын
This is far far better than any other guy on youtube explaining this.
@xyz19891
@xyz19891 9 ай бұрын
Shouldn't you be using unordered_map instead of map? Because time complexity of find() in map is O(logn) whereas in unordered_map is O(1).
@UECAshutoshKumar
@UECAshutoshKumar 6 ай бұрын
Thank you 🙏
@ayushdhoot9784
@ayushdhoot9784 Ай бұрын
@codestorywithMIK dequeue se push and pop o(1) mai hota hai so y we had used DLL rather than dequeue
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
thanks for the explanation. i have done this question before. revisiting it for revision
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Awesome 😇
@DurgaShiva7574
@DurgaShiva7574 7 ай бұрын
won't the list.pop_back() becomes equivalent to traversing the whole list and deleting the last element, which makes the time complexity of this operation as O(n), so it doesn't make difference if we insert the newest element at first, or at last, because either the insertion , or deletion will be still o(n) ??
@adhikari669
@adhikari669 Жыл бұрын
Bhaya dll.pop_back() constant complexity h ? Ya o(n)
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks for asking this Qn. It’s O(1) i.e. CONSTANT You can read more here : cplusplus.com/reference/list/list/pop_back/
@adhikari669
@adhikari669 Жыл бұрын
Bhaya ye function constant time m kaise kaam kr lete h jaise ki vector.size () h ,Bina traverse krre kaise ?
@twi4458
@twi4458 Жыл бұрын
@@adhikari669 kyuki iski internal implementation class se hui hai, to bas apn uska method call kar rhe getsize() type to vo O(1) me return karta
@adhikari669
@adhikari669 Жыл бұрын
@@twi4458Ha ye to m smjha ki class m ek member function h iss naam ka to call kro or answer return krega pr ye Bina traverse kree size kaise pta kr leta h , ye puchna tha..
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Dude this was 🔥. Now, waiting for LFU cache
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
LEGENDARY explanation I had watched other channels as well, this is by far the best explanation
@dilshadalam4546
@dilshadalam4546 6 ай бұрын
You are doing great plz keep doing this .
@sukritiguin5637
@sukritiguin5637 Жыл бұрын
You deserve 100k subs.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot ❤️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Soon this guy will reach 100k I am pretty sure
@Saryupareen
@Saryupareen Жыл бұрын
goat..tecahing
@shreyasrivastava368
@shreyasrivastava368 Жыл бұрын
Hey please continue the graph concept series.. thankyou
@shabananoor9423
@shabananoor9423 Жыл бұрын
++
@khanakpatwari2587
@khanakpatwari2587 8 ай бұрын
why are we specifically pushing the recent ones in the front of the list? Can't we push them at the back instead if we want to?
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Relieved that taj ka POTD ka already video hai is channel ka 🥳🥳🥳🥳
@adhikari669
@adhikari669 Жыл бұрын
Bhaya apni leetcodeprofile share kroo 🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hey Sourav, Find all here github.com/MAZHARMIK
@rahulmaurya6451
@rahulmaurya6451 Жыл бұрын
bhaiya.., i think it's time to take one step forward and go for GFG's POTD also.. what's your thought ?? ,, GFG coders don't get are Quality Explanation on entire KZbin ,, no one explains questions like you,,
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Totally agree. Just need to crunch more time to cover gfg potd. Will soon start this also
@rahulmaurya6451
@rahulmaurya6451 Жыл бұрын
@@codestorywithMIK Yaa.. i know you already working hard and had a very busy schedule and still uploading video's consistently... your dedication is so much appreciable 🙏🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
@rahulmaurya6451 Means a lot 🙏🙏🙏
@shabananoor9423
@shabananoor9423 Жыл бұрын
Perfect explanation. Thanks for the brute force
@abhishekverma7604
@abhishekverma7604 8 ай бұрын
bhya tell me onre thing , thoda awckward question h but dekh lena ek vector m n elements hai to uski run time complexity O(n) hai, but agar hm vector of pair store kr rhe hn to uski complexity O(n^2) honi chahiye naa, because single index store two values in that data structure.
@ShivamMishra-fo1wx
@ShivamMishra-fo1wx Жыл бұрын
what 's the explanation bro great !
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Shivam ❤️❤️
@GeneralistDev
@GeneralistDev Жыл бұрын
Wow daily challenge already avalaible
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes 😇🙏👍🏻 Thank you so much for seeing my video 🙏
@gocrazy6177
@gocrazy6177 2 ай бұрын
Why can't we use only map here ?
@nikhilsatyam4815
@nikhilsatyam4815 8 ай бұрын
why don't we use deque for this problem ?
@yogeshagarwal403
@yogeshagarwal403 8 ай бұрын
in first approach get method is not quadratic, it is linear.
@gulshankumar5552
@gulshankumar5552 Жыл бұрын
bro keep uploading because your way of understanding is very good and communication also, very helpful for me ,
@mayur_madhwani03
@mayur_madhwani03 Жыл бұрын
Small correction push_back in dll is O(1)
@informative180
@informative180 Жыл бұрын
first I was watching striver's video then i saw u also have uploaded on it , automatically finger went there and clicked on your video.🤗
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much for trusting my channel and explanation 😇🙏❤️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
lol same
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
same 2 same
@ridj41
@ridj41 Жыл бұрын
bhai thoda chotti video banaya karo,warna thummbnail se hi bhagna padta hai
@nikhilsatyam4815
@nikhilsatyam4815 8 ай бұрын
kindly add timelines in videos bhaiya.
@ankitachaturvedi2822
@ankitachaturvedi2822 Жыл бұрын
Great explanation! Thank you so much..💫💫
@codestorywithMIK
@codestorywithMIK Жыл бұрын
You are so welcome 😊
@pkm754
@pkm754 Жыл бұрын
cache[key] undeclare hai na??
@animesh414
@animesh414 Жыл бұрын
Please make video on comparator function in C++.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure soon.
@adarshkhetan9424
@adarshkhetan9424 3 ай бұрын
Can we use queue or dequeue ( stl c++) ??
@keertilata20
@keertilata20 Жыл бұрын
finally ye question ab jake meko clear ho gya is video se... thank you so much bhaiya 🥺🥺🥺
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇🙏
@RishabhDhiman-zf5wd
@RishabhDhiman-zf5wd 5 ай бұрын
Great content❤
@adhikari669
@adhikari669 Жыл бұрын
Bhaya kvi aisa hua h ki aapko kisi daily problem m doubt ho or aap discussion section m gye ho. 😅
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed Yes Sourav. But sometimes i hate the solutions provided there because most people don’t explain in detail. So i just try to simplify and produce my own simplified explanation ❤️
@rev_krakken70
@rev_krakken70 4 ай бұрын
Cant i use hashmap + queue here?
@shivsakthi37
@shivsakthi37 Жыл бұрын
U r damn good in explaining things bro 🙏 please could you guide us further on how to improve dsa it would be great help 🍀
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Harsh ❤️❤️ I will soon make a video on guide also. Hope that will help
@shivsakthi37
@shivsakthi37 Жыл бұрын
@@codestorywithMIK it will definitely help me a lot bro thanks looking forward to watch it 🤞
@beenasatyendrapal6405
@beenasatyendrapal6405 8 ай бұрын
You skillfully simplify complex ideas, making learning enjoyable. love from this side....
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️❤️🙏🙏
@informative180
@informative180 Жыл бұрын
bhai u r exceptional , tysm😍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 🙏❤️😇
@pritishpattnaik4674
@pritishpattnaik4674 Жыл бұрын
Absolutely mindblowing , how u constructed the problem by taking it step by step , line by line , BEST of all
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Pritish ❣
@NiteshYadavnitw
@NiteshYadavnitw Ай бұрын
wonderful
@sachinmohanty4577
@sachinmohanty4577 Жыл бұрын
nice ❣
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Sachin.
@k-CE-OmkarPathak
@k-CE-OmkarPathak 8 ай бұрын
Awesome bhaiya op
@sidharthdhiman4522
@sidharthdhiman4522 Жыл бұрын
one of the best explanation for this question but sir one doubt will interviewer would ask us about the internal implementation of this doubly linked list ?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Sidharth. Actually it totally depends if you are a fresher they ask for implementation. But for questions like these, they dont ask internal implementation because this question itself is lengthy
@danishsinghjamwal627
@danishsinghjamwal627 Жыл бұрын
Thankss. Your channel will definitely grow. You have a way with explaining.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Danish ❤️❤️❤️
@shabananoor9423
@shabananoor9423 Жыл бұрын
Come soon
@animesh414
@animesh414 Жыл бұрын
1584. Min Cost to Connect All Points, Make Video on it.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure Animesh. Soon
@niteshk0487
@niteshk0487 Жыл бұрын
class LRUCache { public: class node { public: int key; int val; node *next; node *prev; node(int k, int v) { key = k; val = v; } }; node *head = new node(0,0); node *tail = new node(0,0); int cap =0; unordered_map mp; LRUCache(int capacity) { cap = capacity; head->next = tail; tail->prev = head; } void deleteNode(node *root) { mp.erase(root->key); node *nextNode = root->next; node *prevNode = root->prev; prevNode->next = nextNode; nextNode->prev = prevNode; } void addNode(node *root) { mp[root->key] = root; node *temp = head->next; head->next = root; root->next = temp; temp->prev = root; root->prev = head; } int get(int key) { if(mp.find(key) != mp.end()) { node *curr = mp[key]; int ans = curr->val; deleteNode(curr); addNode(curr); return ans; } return -1; } void put(int key, int value) { node *temp = new node(key, value); if(mp.find(key) != mp.end()) { node *curr = mp[key]; deleteNode(curr); mp.erase(key); } if(mp.size() == cap) { mp.erase(tail->prev->key); deleteNode(tail->prev); } addNode(temp); mp[key] = temp; } };
@niteshk0487
@niteshk0487 Жыл бұрын
@Devender Verma 😂😂😂👍🏻
@EB-ot8uu
@EB-ot8uu 2 ай бұрын
I think this is the only video which helped me understand the approach clearly. thank you so much
@HarshMishra-pu1rn
@HarshMishra-pu1rn Жыл бұрын
6k Soon Bhhaiya Love You
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hope so 🥹 Thank you so much Harsh ❤️🙏🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It’s 6k now - kzbin.infoXxYKzdIp1rI?feature=share Thank you for your well wishes Harsh 😇🙏❤️
@HarshMishra-pu1rn
@HarshMishra-pu1rn Жыл бұрын
@@codestorywithMIK Welcome Now It will be 10K as your way of teaching is excellent
@raftywate
@raftywate 7 ай бұрын
🙇‍♂🙏
@snehagoyal4978
@snehagoyal4978 Жыл бұрын
Thanks for the video sir, loving your content.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks Sneha. Welcome to my channel
@SachinKumar-cd1sg
@SachinKumar-cd1sg Жыл бұрын
The Best Explanation Possible
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Sachin ❤️🙏
@monjuralam7346
@monjuralam7346 Жыл бұрын
Unbelievable explanation. Hats off!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@Rterusjwhhsjsus
@Rterusjwhhsjsus Жыл бұрын
Awesome explanation, loved it !!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@niteshk0487
@niteshk0487 Жыл бұрын
dll create krke fir btana next if possible kyuki stl wala thoda confusion h
@sakshamsharma648
@sakshamsharma648 Жыл бұрын
woaaahhhh explanation!! superb
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@divyangdheer7292
@divyangdheer7292 Жыл бұрын
mera kyo accept nhi ho rha?? ye test case galat hai ["LFUCache","put","put","get","get","get","put","put","get","get","get","get"] [[3],[2,2],[1,1],[2],[1],[2],[3,3],[4,4],[3],[2],[1],[4]] Correct ans: [null,null,null,2,1,2,null,null,3,2,-1,4] Wrong ans by leetcode: [null,null,null,2,1,2,null,null,-1,2,1,4]
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Please share your code
@harikeshsingh93
@harikeshsingh93 Жыл бұрын
amazing explanation.keep it up
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Harikesh ❤️❤️❤️
@DR-mq1le
@DR-mq1le Жыл бұрын
great explanation! thanks!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad it was helpful! 😇
@AlishaKhan-ww3io
@AlishaKhan-ww3io Жыл бұрын
This is one of the finest explanations I have encountered for this problem. Not even famous other youtubers made it this easy
@StudyChannel-zq4bw
@StudyChannel-zq4bw Жыл бұрын
Amazing brother ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@pulastyadas3351
@pulastyadas3351 Жыл бұрын
really awesome explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you ❤️😇
@RajSingh-te1uo
@RajSingh-te1uo Жыл бұрын
Bhai bhai bhai Kya samjhaya hai 💯
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot 😇🙏
@GirishJain
@GirishJain Жыл бұрын
🔥
@GirishJain
@GirishJain Жыл бұрын
awesome explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Girish ❤️
@akki8534
@akki8534 Жыл бұрын
why did't you use unordered_map?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
unordered_map in C++ doesn’t allow keeping pair inside it.
@akki8534
@akki8534 Жыл бұрын
@@codestorywithMIK i used unorderd_map and it passed all test cases. unordered_map does't allow pair as key but it allow as value
@codestorywithMIK
@codestorywithMIK Жыл бұрын
@akki8534 Yes yes. You are right ❤️
@Raj10185
@Raj10185 Жыл бұрын
Good explanation but I found and coded up A better and easy solution using the same DLL and map but i am using a inbuilt STL splice mathod which makes life very easy for transfering of node in linked list to begin code for the same :- class LRUCache { public: // optimal approach +. use an map and doubly linkedlist use STL // use an STL splice function to esy transfer of node in DLL unordered_map mp; // {key , value == iterator of list} list dll; //{key,value} int cap; LRUCache(int capacity) { cap = capacity; } int get(int key) { int value = -1; // first check if is present in map or not if( mp.find(key) == mp.end() ) return -1; else //key exists { auto it = mp.find(key) ; value = it->second->second; //this we need to return // lets place it at front dll.splice(dll.begin(),dll,it->second); } return value; } void put(int key, int value) { // // first check if is present in map or not if( mp.find(key) != mp.end() ) //update its value and move to front { auto it = mp.find(key); //value updation it->second->second = value ; // move to front dll.splice(dll.begin(),dll,it->second); return ; } if(cap==mp.size()) //map size == list size use any one but mp.size is better tc { //need to delete from back //lets store the key to delete we have to delete from map also int key_to_delete = dll.back().first; dll.pop_back(); mp.erase(key_to_delete); } // insert at begin in list and insert in map dll.emplace_front(key,value); //emplace front or push_front but emplace front have better TC mp[key] = dll.begin(); } };```
@sharibsaifi-vw4or
@sharibsaifi-vw4or Жыл бұрын
Excellent explanation understood it very well 👍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Sharib ❤️
LFU Cache - (Microsoft)  :  Explanation➕Live Coding
49:29
codestorywithMIK
Рет қаралды 8 М.
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 12 МЛН
Officer Rabbit is so bad. He made Luffy deaf. #funny #supersiblings #comedy
00:18
Funny superhero siblings
Рет қаралды 13 МЛН
А ВЫ ЛЮБИТЕ ШКОЛУ?? #shorts
00:20
Паша Осадчий
Рет қаралды 9 МЛН
LRU Cache - Twitch Interview Question - Leetcode 146
17:49
NeetCode
Рет қаралды 277 М.
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 97 М.
L18. Implement LRU Cache
24:56
take U forward
Рет қаралды 45 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 118 М.
Restore IP Addresses | Recursive Tree | Diagram | META | Leetcode 93
38:01
LRU Cache -  Explanation, Java Implementation and Demo
23:41
Bhrigu Srivastava
Рет қаралды 17 М.
LRU cache | Leetcode 146 | Hindi
27:26
Codebix
Рет қаралды 11 М.
Imlement LFU Cache | Leetcode(Hard)
19:27
take U forward
Рет қаралды 121 М.
Implement LRU Cache | Leetcode
17:05
take U forward
Рет қаралды 275 М.