Implement LRU Cache | Leetcode
Check our Website:
In case you are thinking to buy courses, please check below:
Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
---------------------------------------------------------------------------------------------------------------------------------------------------- SDE sheet: bit.ly/takeUforward_SDE
Code Implementation Video: • Implement LRU Cache | ...
✅Use coupon-code "TAKEUFORWARD" for getting 15% for all CN courses: aff.codingninjas.com/click?o=...
Watch at 1.25x for better experience ..
---------------------------------------------------------------------------------------------------------------------------
Problem Link: leetcode.com/problems/lru-cache/
C++: github.com/striver79/SDESheet...
Java: github.com/striver79/SDESheet...
---------------------------------------------------------------------------------------------------------------------------
✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgCourse
✅Please Please SUBSKRIIIBEEEEE the new channel: / @striver_79
---------------------------------------------------------------------------------------------------------------------------
✅Placement Series: • Let's give back to the...
✅Placement Series (Arrays, Sorting..): • C++ and Java |Brute-Be...
✅Hashing Playlist: • Two Sum Problem | Leet...
✅Greedy Playlist: • N meetings In One Room...
✅Recursion Playlist: • L8. Combination Sum | ...
✅Graph Playlist: • 3 MAJOR ANNOUNCEMENTS ...
✅Two Pointer Playlist: • 3 SUM | Brute | Better...
✅Binary Search Playlist: • Nth Root of a Number U...
✅LinkedList Playlist: • Reverse a Linked List ...
✅Advanced DS playlist: • Fenwick Tree Tutorial ...
✅Stack&Queue Playlist: • Implementation of Stac...
✅Greedy Algorithms: • N meetings In One Room...
---------------------------------------------------------------------------------------------------------------------------
If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
✅Thumbnail Creator: / rikonakhuli
✅ Striver's Linkedin Profile: /
✅ Instagram: /
✅Connect with us: www.google.com/search?client=... (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
#dsa #striver #leetcode
Пікірлер: 231
DSA Playlists in the comment section ..
@AdityaPrakashSinha
3 жыл бұрын
Please make a video on BCA Students... Roadmap for off campus placement...
@AmanKumarSinhaOfficial
3 жыл бұрын
@@AdityaPrakashSinha yes....
@gamingfingers4523
3 жыл бұрын
@@AdityaPrakashSinha Bruh it's extremely difficult to get a SDE in a product based company by only having BCA try doing MCA yours odds will increase.
@manaswitadatta
3 жыл бұрын
Any chance of top interview question series from LC?
That "just is case" has saperate feeling 💻😂
For some questions even if I have solved the question previously, I do watch your video for crystal clear explanation. Such an amazing teacher you are bhaiya .
@takeUforward
3 жыл бұрын
Thankyou
@Gear5-Luffy
9 ай бұрын
@@takeUforward thankyou ki kya baat ,ye to mera farj hai
@ipizza9941
2 ай бұрын
@@Gear5-Luffy acha bkl
This course have saved my life. thank you so much. i would have never built confidence in solving problems if it wasn't for u
One of the finest explanations of LRU Cache. God bless you Striver and more power to you. Such impeccable energy and knowledge of data structures is rarely seen at tandem.
@aysams2
Жыл бұрын
+++
That much energy you put, motivating me to stay awake and study. Thanks 💖
Great explanation, the only note for (11:23) is that repositioning node in doubly linked list doesn't necessarily require deleting node and creating a new one, it may be enough to update the node (and its neighbors) pointers, thereby preserving address of the node unchanged.
@devanshbhatt7609
Жыл бұрын
Yeah I was thit the same but still not sure if we can do that in all possible edge cases
@aaaaz424
Жыл бұрын
That was my first thought as well. I just dont understand when to use DLL in a problem.
@Anythiny
Жыл бұрын
@@aaaaz424 when u hv to do insertion or deletion in middle of the list. in O(1) time(provided we know where our node is)
@sudhanshusingh-cy9wp
Жыл бұрын
I dont think, this is going to work, since if you just change the address, you will also need to shift all the next nodes from head, that will take O(n) time
Thank You for your explaination videos bhaiya !! Cannot find better explanation than yours in web !!
Your explanation is superb. How beautifully you explain each and every step. Thank you so much bhaiya,
best ever explanation as always !! even I who always try to skip this topic whenever cover up the queue this time watch only concep and implement it on leetcode and after one minor error it works fine BOOST MY CONFIDENCE
Brother, I envy your enthusiasm while teaching. Great video. Thanks a lot.
This video helped me clear 2nd technical round and eventually get placed at a dream company that came to our college 2 days ago. Thank you so much for making such quality content!
If take you forward was not created i would have been literally aiming for 3.5 pkg but because of take you forward i got my concepts cleared and ready to be at product based company with a lot of confidence💯Thanks Striver Bhai✌️🙏
@utkarshsharma6650
2 жыл бұрын
did you get placed?
@namangupta414
Жыл бұрын
No reply it means it's fake😂
@codingalley6229
6 ай бұрын
bhai sabko notification nahi jata, bc kitne negative ho@@namangupta414
Congratulations for 100K subscribers takeUforward truly deserve this and more 🙌
Next level explanation dada
So much energy for teaching bro. Amazing
What a nice way of explanation! More power to you :)
Why are we deleting the nodes and creating new ones when they shift? We can just manipulate the pointers (prev & next) such that the same node in the memory is pointed to by the head (ie is the first element). Open to suggestions but I think that is a solid way to not modify the hashmap on get operations.
The enthusiasm was on different level💯
One of the finest of explanation. Kudos to you for your efforts.
Thank you for this, beautifully explained ❤
Congratulations on 100k bhaiya !!
Your energy 💯.
Great video! thanks. However, the address of the node wouldn't change after deletion. We are just changing the pointers of the nodes in the DLL.
striver did you even thought that you would get so much love from students all over India , from working at google, to giving best content, reaching 500k subs, hatsoff man❤❤
if you are facing TLE, change the get function like below. int get(int _key) { if(ump.find(_key)!=ump.end()){ node* resnode=ump[_key]; int res=resnode->val; resnode->prev->next=resnode->next; resnode->next->prev=resnode->prev; addnode(new node(_key,res)); ump[_key]=head->next; delete(resnode); return res; } return -1; }
@programming_with_niraj.
Жыл бұрын
delete(resnode) should be commented
Nice explanation your videos are really good...please keep on making such videos.
one question, if a node already exists and we are asked to update it, why are we updating the node address in the hashmap since if you observe carefully then only the links and the data of the nodes are changing, the address/reference of the nodes themselves are not changing.
Amazing explanation. Thank you.
You explain soooo well!
"This guy" Best explaination one could find over web!
Love u 3000 man best content for programming in earth !
At first i thought whiteboard is too old school. It might be boring. But after seeing the whole video .I definitely can see your energy level is much higher and thus it becomes more lively and easy to understand. So in short it seems that this style od teaching turns out to be awesome.
OP explanation and energy 👌👌👌🔥🔥🔥
Yesterday I was doing SDE sheet 2, completed stack part-1 & missed doing part 2. Today had interview in one of the best startubs in Bangalore & was asked to design LRUcache (couldn't do completely)
perfect explanation broo...ur doing great
Such a great explanation
truly awesome explaination !! : ) Thank you bro !!
For maintaining the order can we not just change the links so that head.next becomes the node that is getting used and the next for that node and prev are adjusted accordingly ? Why to delete the node and add a new one ?
You are a saviour. You achieve all the success in the world
Best explanation I've seen yet, earned yourself a sub
Amazing explanation.
Do we really need to update the address in Hashmap when moving the node to the front? I believe we could just re-adjust the pointers of the existing node. that way there is no need to update the map while updating the frequency. node address remains same.
@bikrantjajware6062
2 жыл бұрын
same doubt , i think its not necessary to delete the node
@dhanesh.s
2 жыл бұрын
It will take O(n) time to search the node Right?
@adityabalodi1185
2 жыл бұрын
@@dhanesh.s No, you already have the node in hashmap so it will be O(1). So you just use some temp variables and change the previous of the node being accessed(say, currenr) to next of current, and head's next pointer to current. Then you change curr.next to whatever head.next was previously. And obviously maintain the previous of all the nodes.
We can directly store the value in hash map instead of DLL then also it is same right?
Awesome explanation 💯💯💯
Really appreciate your passion you put in explaining....
Nice video, I've a question here - For the operation . get(3), you are trying to delete the entry from the middle of the DLL and insert at the front if my understanding is correct. If that's true, my question is- the time complexity of deleting a node from the middle of a DLL is not 0(1), right?
@Saurabh-lb1uv
Жыл бұрын
It is O(1) only. Using hashmap we know the address of the middle node, Say it's address is x Then all we have to do is: x -> prev -> next = x -> next; x -> next -> prev = x -> prev; delete x; This will remove the node from the DLL. That means it takes O(1) time for deletion.
@rajeshg3570
Жыл бұрын
@@Saurabh-lb1uv thanks for the clarification
Thanks for your Efforts
I ended up solving the question by utilizing the dictionary (hashmap) and doubly linked list data structures. However, I did not know you could solve it easily using an ordered dictionary in Python. Nevertheless, great explanation!
Love the way u explain😍😍
UNDERSTOOD.............Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
such a great explanation bro
Your videos is awesome♥️
your energy is the awesome, keep it up bro..
amazing explanation!!!!!
Brilliant explanation.
Very Nice Explanation....Keep making videos
Explanation and Energy !!!!💯🔥
Best explanation so far🙌
bro ...Apart from DSA , one cute suggestion from ur Die hard fan....Besides lower beard , also grow mustache, Dont cut mustache , let it grow densely.. only then lower beard suits perfectly :-) Love u loads.
Great video, one question can't we implement the same with singly linked list?
How we maintain the min freq in O(1). I mean how we find which is the current least frequency and how we change the Least frequency during get and put can you explain that logic as well ?
@narendranarumugavadivelu8648
2 жыл бұрын
You can always insert the latest node at front and LRU node at rear. So when wanted to remove LRU node just point the rear to previous of current rear node
very nicely explained :)
big thanks bhaiya.. such a nice explanation🤩
One of the best explanation
Thanks Bhaiya! You're the best :D
Good Explanation!
You are a great genius....
great explanation as always....but cant we implement using a doubly ended queue(dequeue) ? it will be much easier and will take lesser space
@TheAnandsingh111
3 жыл бұрын
If you use a queue how will you get an element which is somewhere in between?...it will take more than O(1) time
@mohdhammadsiddiqui7598
2 жыл бұрын
@@TheAnandsingh111 i think using hashmap we can find , queue + hasmap will it work?
@TheAnandsingh111
2 жыл бұрын
@@mohdhammadsiddiqui7598 if you use another hashmap for storing addresses of elements in queue...then it can he done. But time complexity will become O(n) for deleting any element. Because queue does not supports removing an element from between.
@mohdhammadsiddiqui7598
2 жыл бұрын
@@TheAnandsingh111 ohh shit, u are right we can't delete in between elements , now i understood why we use link list because we can rearrange the nodes and their links
@TheAnandsingh111
2 жыл бұрын
@@mohdhammadsiddiqui7598 cool 🙌
thanks for the explanation!
Really great explanation👏
when we are performing get() operation and we are shifting that right after head as it is most recently used. But I am not getting the point of changing addresses of same node coz we are just connecting it at other place but in memory it will be saved at the previous address only. Please clear my doubt...
@takeUforward
3 жыл бұрын
Yes you can improve that point for sure.
@takeUforward
3 жыл бұрын
Yes you can improve that point for sure.
@kunalverma9777
3 жыл бұрын
@@takeUforward Thankyou bhaiya
striver is on fire in this video. 🔥🔥🔥🔥
awesome...................
Nicely explained
Why was this given in Stack and Queues section in SDE sheet?
we can also solve this question easily using combination of (HashMap+LinkedHashMap) in java.
Great bhai..🙏🙏
Great Explanation.... :)
*Thank You so much. Love & Respect always.*
Why you have used Doubly LinkedList. Why not use singly linked list and maintain pointers pointing to start and rear. Please advise.
Very well understood
Amazing !!!
what if you want to put a key and value into the hashmap but the same key and value is already present in the hashmap?
@rohanmahto3123
Жыл бұрын
you just change the position of the node after the head as it is the recently used
Cant be thankful enough man !!
Well explained
Thank you so much striver!
Awesome explanation ❤❤
the finesse of a Google engineer 😍🤩
why create a new address in case of node shifting. Only the previous and next can be updated?
best explanation this ques could ever have
Merci Merci
searching in an unordered map takes o(N) worst case time right? But requirement was to do it in o(1).
@a_05_himanshu_akula31
Жыл бұрын
Access by key ..dont search
Nice explanation 🔥
Can I use queue data structure instead of DLL??
@pankajarora8263
2 жыл бұрын
Same doubt because in sde sheet also this question has been included in queue topic and in gfg also queue is included in topic tags.
Why is this question marked as a stack/queue question in your sheet?? neither stack nor queue is used?
10:19 "it does" hit me hard
brilliant as usual
anyone tell me how i can get itution that the problem can be solved in this way
from functools import lru_cache But well explained 👍👍
@takeUforward
3 жыл бұрын
Not allowed in interview
@user-tx3mo1ez2n
3 жыл бұрын
@@takeUforward ha bhaiya.