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 Жыл бұрын
Aaay Sir 🔥
@AbhijeetKumarAJ Жыл бұрын
yeh pre-requisite hai for LFU cache?
@niteshk0487 Жыл бұрын
@@AbhijeetKumarAJ No bro pre-requisite is LRU
@AbhijeetKumarAJ Жыл бұрын
@@niteshk0487 I AM ASKING is LRU pre-requisite for LFU
@codestorywithMIK Жыл бұрын
Yea this qn should be solved before LFU It will make it easy for all of us to understand LFU Cache
@singerpranavmodi Жыл бұрын
I hope I would have got this channel before. You are really awesome bro!!!
@codestorywithMIK Жыл бұрын
It means a lot to me. Thank you so much 😇🙏
@Auto_Enthusiast Жыл бұрын
awesome explanation , bhai appke smjhane ka kya hi jawaab hai , mtlb shbd km pd jayenge!!!
@codestorywithMIK Жыл бұрын
Means a lot 😇🙏
@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 Жыл бұрын
Indeed . Feels great you mentioned this one ❤️❤️❤️
@AlishaKhan-ww3io Жыл бұрын
This is one of the finest explanations I have encountered for this problem. Not even famous other youtubers made it this easy
@gui-codes2 ай бұрын
This is far far better than any other guy on youtube explaining this.
@wearevacationuncoverers Жыл бұрын
LEGENDARY explanation I had watched other channels as well, this is by far the best explanation
@pritishpattnaik4674 Жыл бұрын
Absolutely mindblowing , how u constructed the problem by taking it step by step , line by line , BEST of all
@codestorywithMIK Жыл бұрын
Thank you so much Pritish ❣
@keertilata20 Жыл бұрын
finally ye question ab jake meko clear ho gya is video se... thank you so much bhaiya 🥺🥺🥺
@codestorywithMIK Жыл бұрын
Thank you so much 😇🙏
@beenasatyendrapal640510 ай бұрын
You skillfully simplify complex ideas, making learning enjoyable. love from this side....
@codestorywithMIK10 ай бұрын
❤️❤️🙏🙏
@souravjoshi2293 Жыл бұрын
Relieved that taj ka POTD ka already video hai is channel ka 🥳🥳🥳🥳
@hameedmulani21 Жыл бұрын
Mind Blowing
@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 Жыл бұрын
Thank you so much for trusting my channel and explanation 😇🙏❤️
@souravjoshi2293 Жыл бұрын
lol same
@wearevacationuncoverers Жыл бұрын
same 2 same
@gulshankumar5552 Жыл бұрын
bro keep uploading because your way of understanding is very good and communication also, very helpful for me ,
@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 Жыл бұрын
Thank you so much Sure thing
@danishsinghjamwal627 Жыл бұрын
Thankss. Your channel will definitely grow. You have a way with explaining.
@codestorywithMIK Жыл бұрын
Thanks a lot Danish ❤️❤️❤️
@souravjoshi2293 Жыл бұрын
Dude this was 🔥. Now, waiting for LFU cache
@EB-ot8uu4 ай бұрын
I think this is the only video which helped me understand the approach clearly. thank you so much
@RajSingh-te1uo Жыл бұрын
Bhai bhai bhai Kya samjhaya hai 💯
@codestorywithMIK Жыл бұрын
Thanks a lot 😇🙏
@shabananoor9423 Жыл бұрын
Perfect explanation. Thanks for the brute force
@floatingpoint7629 Жыл бұрын
thanks for the explanation. i have done this question before. revisiting it for revision
@codestorywithMIK Жыл бұрын
Awesome 😇
@ankitachaturvedi2822 Жыл бұрын
Great explanation! Thank you so much..💫💫
@codestorywithMIK Жыл бұрын
You are so welcome 😊
@DeadCode_Debugs2 ай бұрын
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
@monjuralam7346 Жыл бұрын
Unbelievable explanation. Hats off!!
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@abhishekkarn89187 ай бұрын
Amazing explanation.Way to go brother.
@dilshadalam45468 ай бұрын
You are doing great plz keep doing this .
@gulshanmathur3738Ай бұрын
very welled explain man god bless you...
@snehagoyal4978 Жыл бұрын
Thanks for the video sir, loving your content.
@codestorywithMIK Жыл бұрын
Thanks Sneha. Welcome to my channel
@informative180 Жыл бұрын
bhai u r exceptional , tysm😍
@codestorywithMIK Жыл бұрын
Thank you so much 🙏❤️😇
@sreejasree74273 ай бұрын
Thankyou sir , best explanation
@UECAshutoshKumar8 ай бұрын
Thank you 🙏
@harikeshsingh93 Жыл бұрын
amazing explanation.keep it up
@codestorywithMIK Жыл бұрын
Thank you so much Harikesh ❤️❤️❤️
@SachinKumar-cd1sg Жыл бұрын
The Best Explanation Possible
@codestorywithMIK Жыл бұрын
Thank you so much Sachin ❤️🙏
@closer96893 ай бұрын
Awesome👏👏👏👏....................I was using a Deque😦
@sakshamsharma648 Жыл бұрын
woaaahhhh explanation!! superb
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@Rterusjwhhsjsus Жыл бұрын
Awesome explanation, loved it !!
@codestorywithMIK Жыл бұрын
Thank you so much ❣
@sukritiguin5637 Жыл бұрын
You deserve 100k subs.
@codestorywithMIK Жыл бұрын
Means a lot ❤️
@souravjoshi2293 Жыл бұрын
Soon this guy will reach 100k I am pretty sure
@pulastyadas3351 Жыл бұрын
really awesome explanation
@codestorywithMIK Жыл бұрын
Thank you ❤️😇
@StudyChannel-zq4bw Жыл бұрын
Amazing brother ❤
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@k-CE-OmkarPathak10 ай бұрын
Awesome bhaiya op
@shreyasrivastava368 Жыл бұрын
Hey please continue the graph concept series.. thankyou
@shabananoor9423 Жыл бұрын
++
@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 Жыл бұрын
Absolutely correct Abhijeet ❤️
@mayur_madhwani03 Жыл бұрын
Small correction push_back in dll is O(1)
@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 Жыл бұрын
Thanks a lot Harsh ❤️❤️ I will soon make a video on guide also. Hope that will help
@shivsakthi37 Жыл бұрын
@@codestorywithMIK it will definitely help me a lot bro thanks looking forward to watch it 🤞
@sharibsaifi-vw4or Жыл бұрын
Excellent explanation understood it very well 👍
@codestorywithMIK Жыл бұрын
Thanks a lot Sharib ❤️
@RishabhDhiman-zf5wd7 ай бұрын
Great content❤
@shikharpandya49274 ай бұрын
Thanks a lot sir
@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 Жыл бұрын
Thank you so much for sharing ❤️❤️
@DR-mq1le Жыл бұрын
great explanation! thanks!
@codestorywithMIK Жыл бұрын
Glad it was helpful! 😇
@HarshMishra-pu1rn Жыл бұрын
6k Soon Bhhaiya Love You
@codestorywithMIK Жыл бұрын
Hope so 🥹 Thank you so much Harsh ❤️🙏🙏
@codestorywithMIK Жыл бұрын
It’s 6k now - kzbin.infoXxYKzdIp1rI?feature=share Thank you for your well wishes Harsh 😇🙏❤️
@HarshMishra-pu1rn Жыл бұрын
@@codestorywithMIK Welcome Now It will be 10K as your way of teaching is excellent
@nagmakhan672 Жыл бұрын
Amazing. Thanks a lot Need java code if possible
@NiteshYadavnitw3 ай бұрын
wonderful
@GeneralistDev Жыл бұрын
Wow daily challenge already avalaible
@codestorywithMIK Жыл бұрын
Yes 😇🙏👍🏻 Thank you so much for seeing my video 🙏
@Saryupareen Жыл бұрын
goat..tecahing
@meoww-c3b3 ай бұрын
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); } }
@vladimirprotein96933 ай бұрын
Thanks brother!!
@ayushdhoot97843 ай бұрын
@codestorywithMIK dequeue se push and pop o(1) mai hota hai so y we had used DLL rather than dequeue
@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 Жыл бұрын
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
@animesh414 Жыл бұрын
Please make video on comparator function in C++.
@codestorywithMIK Жыл бұрын
Sure soon.
@sachinmohanty4577 Жыл бұрын
nice ❣
@codestorywithMIK Жыл бұрын
Thank you so much Sachin.
@GirishJain Жыл бұрын
🔥
@GirishJain Жыл бұрын
awesome explanation
@codestorywithMIK Жыл бұрын
Thanks a lot Girish ❤️
@abhishekverma760410 ай бұрын
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 Жыл бұрын
what 's the explanation bro great !
@codestorywithMIK Жыл бұрын
Thank you so much Shivam ❤️❤️
@niteshk0487 Жыл бұрын
dll create krke fir btana next if possible kyuki stl wala thoda confusion h
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 Жыл бұрын
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 Жыл бұрын
@@codestorywithMIK Got it bhaiya thanks for the reply 🤩
@khanakpatwari258710 ай бұрын
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?
@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 Жыл бұрын
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 😇🙏❤️
@DurgaShiva75749 ай бұрын
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 Жыл бұрын
Bhaya kvi aisa hua h ki aapko kisi daily problem m doubt ho or aap discussion section m gye ho. 😅
@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 ❤️
@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 Жыл бұрын
You are a Life saviour.
@nagmakhan672 Жыл бұрын
Wow. tysm
@JJ-tp2dd Жыл бұрын
thank you bhai and Nagma
@xyz1989111 ай бұрын
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).
@oqant0424Ай бұрын
U made it a "Halwa" seriously
@adarshkhetan94245 ай бұрын
Can we use queue or dequeue ( stl c++) ??
@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 Жыл бұрын
Totally agree. Just need to crunch more time to cover gfg potd. Will soon start this also
@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 Жыл бұрын
@rahulmaurya6451 Means a lot 🙏🙏🙏
@gocrazy61774 ай бұрын
Why can't we use only map here ?
@ridj41 Жыл бұрын
bhai thoda chotti video banaya karo,warna thummbnail se hi bhagna padta hai
@adhikari669 Жыл бұрын
Bhaya dll.pop_back() constant complexity h ? Ya o(n)
@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 Жыл бұрын
Bhaya ye function constant time m kaise kaam kr lete h jaise ki vector.size () h ,Bina traverse krre kaise ?
@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 Жыл бұрын
@@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..
@raftywate9 ай бұрын
🙇♂🙏
@nikhilsatyam481510 ай бұрын
why don't we use deque for this problem ?
@rev_krakken706 ай бұрын
Cant i use hashmap + queue here?
@yogeshagarwal40310 ай бұрын
in first approach get method is not quadratic, it is linear.
@nit8826 Жыл бұрын
Sir,Issi ke sath LFU ka bhi dekh lete toh kaam ho jata
@codestorywithMIK Жыл бұрын
LFU video uploaded in this playlist 🙏😇❤️
@pkm754 Жыл бұрын
cache[key] undeclare hai na??
@shabananoor9423 Жыл бұрын
Come soon
@adhikari669 Жыл бұрын
Bhaya apni leetcodeprofile share kroo 🙏
@codestorywithMIK Жыл бұрын
Hey Sourav, Find all here github.com/MAZHARMIK
@nikhilsatyam481510 ай бұрын
kindly add timelines in videos bhaiya.
@practical-mediaАй бұрын
Bhai why u did not uded queue
@akki8534 Жыл бұрын
why did't you use unordered_map?
@codestorywithMIK Жыл бұрын
unordered_map in C++ doesn’t allow keeping pair inside it.
@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 Жыл бұрын
@akki8534 Yes yes. You are right ❤️
@animesh414 Жыл бұрын
1584. Min Cost to Connect All Points, Make Video on it.
@codestorywithMIK Жыл бұрын
Sure Animesh. Soon
@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 Жыл бұрын
Please share your code
@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(); } };```