LRU cache | EP 22

  Рет қаралды 24,778

Fraz

Fraz

Күн бұрын

Пікірлер: 74
@facttecher3039
@facttecher3039 6 ай бұрын
Finally i understand the logic behind this problem. I really like your way of explaination . This type of explaination I don't get anywhere. Keep it up sir ji.
@desihiphop4998
@desihiphop4998 3 жыл бұрын
Bhiaya poori playlist ka sabse accha question laga pehle bhe kr rakha tha but ni kr paaya is baar but ab acche se smjh gya !!!!! Hero ho bhaiya aap !!!🖤🖤
@dp-ev5me
@dp-ev5me 3 жыл бұрын
done for this weekend, looking forward to next. Thank you sir.
@mohammadfraz
@mohammadfraz 3 жыл бұрын
Good job 🔥
@ece116pranaykumar4
@ece116pranaykumar4 2 жыл бұрын
getting TLE now
@Wanna_be_01
@Wanna_be_01 Жыл бұрын
1. BRUTE FORCE APPROACH(Using Queue + HashMap):- ------------------------------------------------------------------------------------------ class LRUCache { public: unordered_map m, cnt; queue q; int n; LRUCache(int capacity){ n = capacity; } int get(int key) { if (cnt.find(key) == cnt.end()) return -1; q.push(key); cnt[key]++; return m[key]; } void put(int key, int value) { q.push(key); cnt[key]++; m[key] = value; while (cnt.size() > n) { int cur = q.front(); q.pop(); if (cnt[cur]-- == 1) cnt.erase(cur); } } }; // Time Complexity = O(n+m) // Space Complexity = O(n+m) 2. OPTIMIZED APPROACH(Using DLL + HashMap) - By Fraz Bhaiya (from this lecture):- ---------------------------------------------------------------------------------------------------------- class LRUCache { public: unordered_map m; unordered_map address; list l; int cap; int siz; LRUCache(int capacity) { cap = capacity; siz = 0; } int get(int key) { if(m.find(key) == m.end()) return -1; auto it = address[key]; l.erase(it); address.erase(key); l.push_front(key); address[key] = l.begin(); return m[key]; } void put(int key, int value) { if(m.find(key) != m.end()){ auto it = address[key]; l.erase(it); address.erase(key); m.erase(key); siz--; } if(siz == cap){ int del = l.back(); l.pop_back(); address.erase(del); m.erase(del); siz--; } siz++; l.push_front(key); address[key] = l.begin(); m[key] = value; } }; // Time Complexity = O(1) // Space Complexity = O(Capacity) ----------------------------------------------------------------------------------------------------------------- Enjoy Guys !!! ❤😉 I wrote here for revision purpose in future but you can use too for revision :) And Thank to Fraz Bhaiya 🙏🥰❣🔥for whole content :)
@ayushbhagwat3440
@ayushbhagwat3440 2 жыл бұрын
It's giving TLE 21/22 Test cases passed. I even tried using unordered_map but still no effect. Last executed input: ["LRUCache","get","get","put","get","put","put","put","put","get","put"] [[1],[6],[8],[12,1],[2],[15,11],[5,2],[1,15],[4,2],[5],[15,15]]
@balakrishnanr648
@balakrishnanr648 2 жыл бұрын
so what u did?
@gantayat
@gantayat 2 жыл бұрын
Keep submitting, it will pass randomly xD (shorturl.at/AU157 )
@mgdesire9255
@mgdesire9255 3 жыл бұрын
finally S2 done...looking forward Bhaiya can't wait for upcoming season.
@mohammadfraz
@mohammadfraz 3 жыл бұрын
aaega aaega
@mohdsaadalikhan7209
@mohdsaadalikhan7209 3 жыл бұрын
time: o(1) average case , o(n) worst case (as in unordered map , collisions may increase the time for search or insert to o(n) ) space: o(n) correct me if i am wrong?
@abhayrai1724
@abhayrai1724 2 жыл бұрын
Bhaiya aab aap ese ese educational videos kyu nhi dal rhe missing those days jb ache ache ques solve krwate the aap kafi easily .... Plz laut aao purane wale bhaiya 😍🤗
@_inspireverse___
@_inspireverse___ Жыл бұрын
Well explained. Thank you so much 👍
@baap6382
@baap6382 3 жыл бұрын
Sir your videos are absolute gold. Keep posting. I am in final year. Wished I was born 2-3 years later
@mohammadfraz
@mohammadfraz 3 жыл бұрын
Thanks a lot for watching
@vikassinghbisht7305
@vikassinghbisht7305 8 ай бұрын
u r best fraz bhaiya
@ritulraj6119
@ritulraj6119 Жыл бұрын
Great Explanation 👌
@shashankpandey4387
@shashankpandey4387 Жыл бұрын
Both get() and put() will have time complexity of O(n) , because you are using erase() which take O(n) time Random Access is not allowed in list try to implement in O(1) as asked in question @frazmohammad Thanks
@mdnadeem6343
@mdnadeem6343 3 жыл бұрын
TC: O(log n) for both get and put function SC: O(n)
@sudhanshuranjan4392
@sudhanshuranjan4392 2 жыл бұрын
Can you please explain how? I guess TC will be O(n logn).
@balakrishnanr648
@balakrishnanr648 2 жыл бұрын
@@sudhanshuranjan4392 he has given amortized TC i mean avg one for all n ops.
@louistomlinson3367
@louistomlinson3367 2 жыл бұрын
if anybody is wondering how will be the java code, here it is class LRUCache { HashMap map; Node head; Node tail; int capacity; public LRUCache(int capacity) { head = new Node(0, 0); tail = new Node(0, 0); this.capacity = capacity; map = new HashMap(); head.next = tail; tail.prev = head; } public int get(int key) { if (map.containsKey(key)) { Node node = map.get(key); delete(node); insert(node); return node.value; } else { return -1; } } public void put(int key, int value) { if (map.containsKey(key)) { Node node = map.get(key); node.value = value; map.put(key, node); delete(node); insert(node); } else if (map.size() < capacity) { Node newNode = new Node(key, value); map.put(key, newNode); insert(newNode); } else { Node newNode = new Node(key, value); Node toDelete = tail.prev; map.remove(toDelete.key); delete(toDelete); insert(newNode); map.put(key, newNode); } } private void delete(Node node) { Node prev = node.prev; Node next = node.next; prev.next = next; next.prev = prev; } private void insert(Node node) { Node headNext = head.next; head.next = node; node.prev = head; node.next = headNext; headNext.prev = node; } } class Node { int key; int value; Node next; Node prev; public Node(int key, int value) { this.key = key; this.value = value; next = prev = null; } }
@devendrabhuarya4917
@devendrabhuarya4917 Жыл бұрын
class LRUCache { public: unordered_map values; unordered_mapm; list l; int capacity,size; LRUCache(int capacity) { this->capacity=capacity; size=0; } int get(int key) { if(m.find(key)==m.end()) return -1; auto it=m[key]; l.erase(it); l.push_front(key); m[key]=l.begin(); return values[key]; } void put(int key, int value) { if(m.find(key)!=m.end()) { auto it=m[key]; l.erase(it); l.push_front(key); m[key]=l.begin(); values[key]=value; return; } if(size
@HarshRaj-kp7gk
@HarshRaj-kp7gk 3 жыл бұрын
sir, please make a video on (computer network) preparation in 3 days for placement sir , "you forgot it this subject to make a video " its humble request sir please make a video as soon as possible
@RitikGupta-uo2te
@RitikGupta-uo2te 2 жыл бұрын
Gate Smashers se jaake pdle bhai aur saath ke saath notes likhlio
@lalit-singh-bisht
@lalit-singh-bisht Жыл бұрын
i was not expecting a stl solution
@manishvijay6794
@manishvijay6794 2 жыл бұрын
Awesome content 🔥🔥
@anupambiswas6148
@anupambiswas6148 2 жыл бұрын
Thank you sir
@krishnaradhey2814
@krishnaradhey2814 Жыл бұрын
fraz bhaiya if i am implementing this question in the opposite of this approach means that for least recently used i am putting nodes on right side end and for most recently used (to be pop) on the left side end IT IS GIVING TLE BY THIS REVERSE APPROACH ???CAN ANYONE EXPLAIN WHY
@mdnadeem6343
@mdnadeem6343 3 жыл бұрын
zbrdst bhaiya
@mohammadfraz
@mohammadfraz 3 жыл бұрын
Thanks Nadeem
@utkarshsingh5390
@utkarshsingh5390 3 жыл бұрын
Season 2 done bhaiya
@guptashashwat
@guptashashwat 2 жыл бұрын
G phat gyi karte karte but excellent explanation!
@Sai-fp3tu
@Sai-fp3tu 3 жыл бұрын
Overall linked list ka kitne videos ayenge bhaiya? Btw Awesome teaching 🔥🔥
@mohammadfraz
@mohammadfraz 3 жыл бұрын
About 30
@SunnyGupta00
@SunnyGupta00 3 жыл бұрын
Reach++ 🔥
@vivekmahindrakar2996
@vivekmahindrakar2996 2 жыл бұрын
Can we use Stacks where the most recently used is the top and lru is at the bottom???
@editorera239
@editorera239 3 жыл бұрын
Thnx bhaiya In series m linked list complete ho jaega na overall I mean
@mohammadfraz
@mohammadfraz 3 жыл бұрын
A-Z khatam ho jaega
@editorera239
@editorera239 3 жыл бұрын
@@mohammadfraz yee thnx bhaiya
@souravkumar5120
@souravkumar5120 2 жыл бұрын
I dont know why but I am getting tle.
@bitrish34
@bitrish34 2 жыл бұрын
use ordered_map instead of map it will pass all test case
@shivanshgupta4342
@shivanshgupta4342 2 жыл бұрын
@@bitrish34 i am using unordered map and it is still giving me tle
@vasuoberoi1810
@vasuoberoi1810 2 жыл бұрын
which whiteboard u are using?
@rishupatel7285
@rishupatel7285 2 жыл бұрын
Is anyone getting TLE For this solution
@deepakprajapat3897
@deepakprajapat3897 3 жыл бұрын
Sir STL full concept ka uper aap video kab upload karoge Plzzzz
@mohammadfraz
@mohammadfraz 3 жыл бұрын
Bana denge
@amit0sunny
@amit0sunny 2 жыл бұрын
if u talk about thinking from scratch then start with a basic solution, its easy to remember a solution and then explain it in here. I don't see how u started with a basic concept, in fact the first thing u said was LinkedList and hashmap , do u think we all should gobble up the solution to each problem?
@tech_wizard9315
@tech_wizard9315 3 жыл бұрын
By when you will upload entire playlist
@mohammadfraz
@mohammadfraz 3 жыл бұрын
Jaldi hi
@madhabkafle8072
@madhabkafle8072 3 жыл бұрын
🔥🔥🔥
@mohammadfraz
@mohammadfraz 3 жыл бұрын
❤️❤️
@TekaneChaitanya
@TekaneChaitanya 3 жыл бұрын
Please make videos using JAVA
@SunnyGupta00
@SunnyGupta00 3 жыл бұрын
It is very similar to c++ (.) Ki jaga (->) ko replace kar do
@MdFaisal-kj1ck
@MdFaisal-kj1ck 3 жыл бұрын
Bhai linked list k baad STL please ASAP.🙏🙏
@mohammadfraz
@mohammadfraz 3 жыл бұрын
yes
@code7434
@code7434 2 жыл бұрын
Faraz please share your submission or please do resubmit your code as it is giving TLE
@bitrish34
@bitrish34 2 жыл бұрын
use ordered_map instead of map it will pass all test case
@yajatdhawan1865
@yajatdhawan1865 2 жыл бұрын
bro its giving TLE
@bitrish34
@bitrish34 2 жыл бұрын
use ordered_map instead of map it will pass all test case
@shubhangverma4478
@shubhangverma4478 2 жыл бұрын
@@bitrish34 not working
@rishabhbatra3022
@rishabhbatra3022 3 жыл бұрын
bhaiya ye jo declarations globally kri h apne interviwer m aese krna thik rehta h ya as a paramter pass krein
@nitunsingh6986
@nitunsingh6986 3 жыл бұрын
U can also make them private within the class
@nitunsingh6986
@nitunsingh6986 3 жыл бұрын
Btw default in C++ they are Private
@harendrameghwal1120
@harendrameghwal1120 2 жыл бұрын
ye code abhi runtime error de rha h leetcode pe 20/22 test cases.
@bitrish34
@bitrish34 2 жыл бұрын
use ordered_map instead of map it will pass all test case
@sarimkhan9897
@sarimkhan9897 2 жыл бұрын
@@bitrish34 not working
@helloUser12373
@helloUser12373 2 жыл бұрын
your solution is not accepted on gfg. Test cases are failing.
@Sonu-tg6tg
@Sonu-tg6tg 3 жыл бұрын
Please make videos using JAVA
LRU Cache - Twitch Interview Question - Leetcode 146
17:49
NeetCode
Рет қаралды 302 М.
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,7 МЛН
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
Reality of my 62LPA package
10:06
Fraz
Рет қаралды 479 М.
LRU Cache | HashMap | Doubly LinkedList
1:08:10
Pepcoding
Рет қаралды 13 М.
NVIDIA’s New AI: Stunning Voice Generator!
6:21
Two Minute Papers
Рет қаралды 76 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 17 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 119 М.
Winning Google Kickstart Round A 2020 + Facecam
17:10
William Lin (tmwilliamlin168)
Рет қаралды 10 МЛН
LRU Cache | Brute Force | Optimal | Detailed | Leetcode 146
40:32
codestorywithMIK
Рет қаралды 18 М.
Implement LRU Cache | Leetcode
17:05
take U forward
Рет қаралды 280 М.