LFU Cache - (Microsoft) : Explanation➕Live Coding

  Рет қаралды 8,657

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 76
@nagmakhan672
@nagmakhan672 Жыл бұрын
Debate is over. This guy is the GOAT
@shikharpandya4927
@shikharpandya4927 4 ай бұрын
Best Explaination for LFU😇
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Well yeah, as mentioned by others, this guy is the G.O.A.T.
@Raj10185
@Raj10185 Жыл бұрын
question bhut jyda tagda hai bhut sare corner case handle karne hai aur dimag me rkhne hai :) ye single question se hi 1-2 round interview ek clear ho jyega pakka maza aaya explanation bdhiya tha :)
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 🙏❤️😇
@PrinceKumar-eq3px
@PrinceKumar-eq3px Жыл бұрын
wow !! such a great explaination !! you deserve 100Million subscriber bro !!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot. Thank you so much 🙏😇❤️
@iWontFakeIt
@iWontFakeIt Жыл бұрын
the way you explained the question at the start of the video, i coded it myself, since i got a lot of intuition from the lru cache video, i did not have to watch this video full, only first 8-10 minutes were helpful
@iWontFakeIt
@iWontFakeIt Жыл бұрын
my AC code: // Leetcode 460 class LFUCache { public: // we have to store frequency as well as maintain lru style for same frequency elements map freq; // stores unordered_map hash; // stores int capacity; LFUCache(int capacity) { this->capacity = capacity; } int get(int key) { if(hash.find(key)!=hash.end()){ // found auto it = hash[key].first; int f = hash[key].second; int value = hash[key].first->second; // since element is accessed, freq increases and also its position in // dll will move forward (to the begin) int new_f = f+1; // erase old value freq[f].erase(it); // check if current freq dll becomes empty if(freq[f].empty()) freq.erase(f); // put the key with new_f to new list freq[new_f].push_front({key, value}); // update the new address in hash hash[key] = {freq[new_f].begin(), new_f}; return value; }else{ // not found return -1; } } void put(int key, int value) { // check if already exists if(hash.find(key)!=hash.end()){ // found auto it = hash[key].first; int f = hash[key].second; int oldValue = hash[key].first->second; // since element is accessed, freq increases and also its position in // dll will move forward (to the begin) int new_f = f+1; // erase the old value freq[f].erase(it); // check if current freq dll becomes empty if(freq[f].empty()) freq.erase(f); // put the key with new_f to new list, with new value provided freq[new_f].push_front({key, value}); // update the new address in hash hash[key] = {freq[new_f].begin(), new_f}; return; } // if not already exists else{ if(hash.size() + 1 first; int keyToBeRemoved = it->second.back().first; // remove from freq it->second.pop_back(); if(it->second.empty()) freq.erase(f); // remove from hash hash.erase(keyToBeRemoved); // insert new element freq[1].push_front({key, value}); hash[key] = {freq[1].begin(), 1}; } } } }; /** * Your LFUCache object will be instantiated and called as such: * LFUCache* obj = new LFUCache(capacity); * int param_1 = obj->get(key); * obj->put(key,value); */
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am so so happy to hear that. Keep it going 💪💪💪❤️
@devangsehgal
@devangsehgal 4 ай бұрын
G.O.A.T TEACHER hai bhai 😎
@UECAshutoshKumar
@UECAshutoshKumar 8 ай бұрын
Thank you 😊
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
You're welcome 😊
@vishalkurve93
@vishalkurve93 5 ай бұрын
Bhaiya Kamaal ho aap.Mik bhaiya zindabad!
@saurabhN1393
@saurabhN1393 Жыл бұрын
Thanks again for the awesome explanation. You are very good at breaking down complex problems into easy solutions. One request can you also provide leetcode weekly contests explanations and how to approach questions during limited time since that is what interview OA simulate. This will help tremendously.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will plan soon about that. Thank you so much ❤️❤️
@abhinavnarang4369
@abhinavnarang4369 8 ай бұрын
very good exp brother. Keep the good work
@snehagoyal4978
@snehagoyal4978 Жыл бұрын
GOAT indeed✨🔥
@divyabharathi5324
@divyabharathi5324 7 ай бұрын
superb brother..one small suggestion for beginners instead of freq.erase(freq.begin()->first); we can directly do a freq.erase(1); because of we always try to erase the one with lowest counter..other than this suggestion its really helpful. Good job
@shabananoor9423
@shabananoor9423 Жыл бұрын
Seriously 💥🔥🔥🔥
@lazyhead1276
@lazyhead1276 Жыл бұрын
crazy explaination sir
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@SONALIKUMARI-is9jc
@SONALIKUMARI-is9jc Жыл бұрын
Hello sir; we've been waiting for your videos
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will be back in no time. Soooon. Mentioned here : kzbin.info/www/bejne/l3iXl62pe86llZI Thanks for trusting my channel
@expert_solver
@expert_solver Жыл бұрын
Very nice & detail explanation.🥰❤ Thank you soo much because i saw many video but i understand from your video, concept as well as code both part are hard of this problem
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much. Means a lot to me ❤️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
The wait is over 🔥🔥🔥
@ipranavprashant
@ipranavprashant 5 ай бұрын
9:08 naughty ho rha h naughty😂
@tutuimam3381
@tutuimam3381 Жыл бұрын
Thanks a lot
@nagmakhan672
@nagmakhan672 Жыл бұрын
I believe by the end of this video explanation, I will be able to answer who is the real Goat of DSA on KZbin 😅 Starting now
@mudassarnazar1663
@mudassarnazar1663 Жыл бұрын
❤❤❤❤
@Rider12374
@Rider12374 14 күн бұрын
>>G.O.A.T Guy........
@thekindspill
@thekindspill Жыл бұрын
Best channel ever
@nishantdehariya5769
@nishantdehariya5769 7 ай бұрын
awesome
@rishabhjain5281
@rishabhjain5281 2 ай бұрын
I got this same problem in my on campus online assignment for the company named avelera
@prathamjiwnani6419
@prathamjiwnani6419 2 ай бұрын
which college??
@niteshk0487
@niteshk0487 Жыл бұрын
Wait is over 😅
@AdritoDey
@AdritoDey Жыл бұрын
Please start uploading contest videos atleast the 3rd and the 4th problem. if possible can you upload weekly contest 330?
@karthikeysharma9161
@karthikeysharma9161 Жыл бұрын
🔥
@babluvishwakarma9898
@babluvishwakarma9898 Жыл бұрын
good job sir
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@shabananoor9423
@shabananoor9423 Жыл бұрын
The GOAT
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
Thank you 💓
@gurnishsingh257
@gurnishsingh257 Жыл бұрын
this is a nice video tbh
@aftereffectstraining7668
@aftereffectstraining7668 Жыл бұрын
❤🔥
@AlishaKhan-ww3io
@AlishaKhan-ww3io Жыл бұрын
G.O.A.T
@gulshanmathur3738
@gulshanmathur3738 29 күн бұрын
Again nice explanation, how did you understand this enormous problem?
@monjuralam7346
@monjuralam7346 Жыл бұрын
Thanks Mazhar; very well explained. Will the time complexity be O(logN) where N is the number of operations; and this is mainly to maintain ordered map?
@ektuhaso
@ektuhaso Жыл бұрын
Explanation ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇❤️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Itni mehnat kaise karte ho bhai 🥺
@pallabghosh3840
@pallabghosh3840 4 ай бұрын
Sir I'm confused about the size of cache. Everytime we insert at 1 , then only size is increased(size
@AkashKumarTiwary-u4b
@AkashKumarTiwary-u4b 6 ай бұрын
Bro is God
@AkashKumarTiwary-u4b
@AkashKumarTiwary-u4b 6 ай бұрын
Broooooo aap to hazaribagh se ho 😭😭 damnnnn mere Ghar ke pass
@AryanTomar_2025
@AryanTomar_2025 Жыл бұрын
Sir pls upload binary tree cameras if possible.
@mohanmanis
@mohanmanis Жыл бұрын
Which application you are using to create the tutorial? It would be great to know.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Default note app on ipad
@invinciblearmyvlogsshorts7911
@invinciblearmyvlogsshorts7911 Жыл бұрын
man..sir how do you keep track of so many things at once?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
😇❤️🙏 just trying my best to give the best content on youtube ❤️
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Here is the Java solution class LFUCache { int capacity; int curSize; int minFrequency; Map map; Map freq; public LFUCache(int capacity) { this.capacity = capacity; this.curSize = 0; this.minFrequency = 0; this.map = new HashMap(); this.freq = new HashMap(); } public int get(int key) { DLLNode curNode = map.get(key); if(curNode == null) return -1; updateNode(curNode); return curNode.val; } public void put(int key, int value) { if(capacity == 0) return; if(map.containsKey(key)){ DLLNode curNode = map.get(key); curNode.val = value; updateNode(curNode); } else{ curSize++; if(curSize > capacity){ DoubleLinkedList minFreqList = freq.get(minFrequency); map.remove(minFreqList.tail.prev.key); minFreqList.removeNode(minFreqList.tail.prev); curSize--; } minFrequency = 1; DLLNode newNode = new DLLNode(key, value); DoubleLinkedList curList = freq.getOrDefault(1, new DoubleLinkedList()); curList.addNode(newNode); freq.put(1, curList); map.put(key, newNode); } } public void updateNode(DLLNode curNode){ int curFreq = curNode.frequency; DoubleLinkedList curList = freq.get(curFreq); curList.removeNode(curNode); if(curFreq == minFrequency && curList.listSize == 0){ minFrequency++; } curNode.frequency++; DoubleLinkedList newList = freq.getOrDefault(curNode.frequency, new DoubleLinkedList()); newList.addNode(curNode); freq.put(curNode.frequency, newList); } } class DLLNode { int key; int val; int frequency; DLLNode prev; DLLNode next; DLLNode(int _key, int _val){ this.key = _key; this.val = _val; this.frequency = 1; } } class DoubleLinkedList { int listSize; DLLNode head; DLLNode tail; DoubleLinkedList(){ this.listSize = 0; this.head = new DLLNode(0,0); this.tail = new DLLNode(0,0); head.next = tail; tail.prev = head; } public void addNode(DLLNode curNode){ DLLNode nextNode = head.next; curNode.next = nextNode; curNode.prev = head; head.next = curNode; nextNode.prev = curNode; listSize++; } public void removeNode(DLLNode curNode){ DLLNode prevNode = curNode.prev; DLLNode nextNode = curNode.next; prevNode.next = nextNode; nextNode.prev = prevNode; listSize--; } }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️❤️
@pradipacharjee4915
@pradipacharjee4915 2 ай бұрын
For the frequency map , you should consider unordered_map. Ordered map will not help to achive o(1)
@abhinavnarang4369
@abhinavnarang4369 8 ай бұрын
One question what is the time complexity of both these functions?
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Time complexity analysis: 1. get(int key): - Searching for a key in the unordered_map `mp` takes O(1) time. - Making the key most frequently used involves erasing and inserting elements in a list, which takes O(1) time since we have iterators. - Overall time complexity: O(1). 2. put(int key, int value): - Checking if the key exists in the unordered_map `mp` takes O(1) time. - Updating the value and making the key most frequently used takes O(1) time. - If the capacity is not reached, inserting a new element in the list takes O(1) time. - If the capacity is reached, finding the least frequently used key and erasing it takes O(log n) time in the worst case due to the use of a map. - Overall time complexity: O(1) for insertion if capacity is not reached, O(log n) for deletion if capacity is reached. The overall time complexity of `get` and `put` operations is O(1) amortized time, where amortized time accounts for the occasional worst-case deletion in `put` when the capacity is reached. Space complexity analysis: 1. The unordered_map `mp` stores the key and the iterator pointing to the corresponding vector, requiring O(n) space, where n is the number of keys. 2. The map `freq` stores the frequency and the list of vectors associated with each frequency, requiring O(n) space. 3. Each vector in the lists contains three integers, requiring O(3n) space. 4. Therefore, the overall space complexity is O(n).
@MakeuPerfect
@MakeuPerfect Жыл бұрын
R a bhaiya kab aayoge jaldi ajao problem samajh nahi a raha
@ec039venom4
@ec039venom4 Жыл бұрын
I am waiting for your videos. Maza nhi a ra questions m
@avinashvishwakarma6488
@avinashvishwakarma6488 Жыл бұрын
bhai gfg ki potd bhi start karo atleast hard wale ki video daalo
@divyangdheer7292
@divyangdheer7292 Жыл бұрын
😭😭😭 Are bhaiya mene LRU wali video dekhli or mereko laga ki wo aaj ka leetcode daily challenge hai Or mai uska code LFU mai submit kr rha tha Mereko laga test case galat hai Or meri streak tut gyi 😭😭
@shashwatchawla
@shashwatchawla Жыл бұрын
Isses streak kaise tut skti hai ?
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
It won't break your streak. You can still submit this code in LFU and the streak remains
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Don’t worry, wrong submission doesn’t break streak.
@manishasaini307
@manishasaini307 Жыл бұрын
bhaiya kha gye😐
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Soon be back kzbin.info/www/bejne/l3iXl62pe86llZI
@mohitsinha1994
@mohitsinha1994 5 ай бұрын
since you are using ordered_map for Frequency, doesn't TC becomes O(logn) for accessing this frequency map?
@nishant7083
@nishant7083 Жыл бұрын
Bro juss forgot me
@MitaliNeerPatel
@MitaliNeerPatel 3 ай бұрын
try to use english bro. helps world wide community.
Design Browser History  | Made Easy | META | Leetcode 1472
14:14
codestorywithMIK
Рет қаралды 3,7 М.
LRU Cache | Brute Force | Optimal | Detailed | Leetcode 146
40:32
codestorywithMIK
Рет қаралды 18 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 183 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 7 МЛН
Prefix Sum in 4 minutes | LeetCode Pattern
4:13
AlgoMasterIO
Рет қаралды 4,3 М.
LFU Cache: LeetCode (Design Problem)
22:43
Shivank Goel
Рет қаралды 10 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 18 М.
NVIDIA’s New AI: Stunning Voice Generator!
6:21
Two Minute Papers
Рет қаралды 78 М.
LRU Cache - Twitch Interview Question - Leetcode 146
17:49
NeetCode
Рет қаралды 302 М.
Imlement LFU Cache | Leetcode(Hard)
19:27
take U forward
Рет қаралды 128 М.
Design HashSet | Full Details | GOOGLE | Leetcode-705 | Live Code
26:17
codestorywithMIK
Рет қаралды 4,1 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 183 МЛН