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

  • @takeUforward
    @takeUforward3 жыл бұрын

    DSA Playlists in the comment section ..

  • @AdityaPrakashSinha

    @AdityaPrakashSinha

    3 жыл бұрын

    Please make a video on BCA Students... Roadmap for off campus placement...

  • @AmanKumarSinhaOfficial

    @AmanKumarSinhaOfficial

    3 жыл бұрын

    @@AdityaPrakashSinha yes....

  • @gamingfingers4523

    @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

    @manaswitadatta

    3 жыл бұрын

    Any chance of top interview question series from LC?

  • @anuraggoswami3534
    @anuraggoswami35343 жыл бұрын

    That "just is case" has saperate feeling 💻😂

  • @tulika2863
    @tulika28633 жыл бұрын

    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

    @takeUforward

    3 жыл бұрын

    Thankyou

  • @Gear5-Luffy

    @Gear5-Luffy

    9 ай бұрын

    @@takeUforward thankyou ki kya baat ,ye to mera farj hai

  • @ipizza9941

    @ipizza9941

    2 ай бұрын

    @@Gear5-Luffy acha bkl

  • @ankushrai3155
    @ankushrai31552 жыл бұрын

    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

  • @swaroopchakraborty3488
    @swaroopchakraborty3488 Жыл бұрын

    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

    @aysams2

    Жыл бұрын

    +++

  • @binod3204
    @binod32043 жыл бұрын

    That much energy you put, motivating me to stay awake and study. Thanks 💖

  • @eldarzakirov5571
    @eldarzakirov5571 Жыл бұрын

    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

    @devanshbhatt7609

    Жыл бұрын

    Yeah I was thit the same but still not sure if we can do that in all possible edge cases

  • @aaaaz424

    @aaaaz424

    Жыл бұрын

    That was my first thought as well. I just dont understand when to use DLL in a problem.

  • @Anythiny

    @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

    @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

  • @bookalicious9849
    @bookalicious98493 жыл бұрын

    Thank You for your explaination videos bhaiya !! Cannot find better explanation than yours in web !!

  • @mousumikundu2680
    @mousumikundu2680 Жыл бұрын

    Your explanation is superb. How beautifully you explain each and every step. Thank you so much bhaiya,

  • @sagartaak4017
    @sagartaak4017 Жыл бұрын

    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

  • @ayanSaha13291
    @ayanSaha132917 ай бұрын

    Brother, I envy your enthusiasm while teaching. Great video. Thanks a lot.

  • @saptarshidas488
    @saptarshidas488 Жыл бұрын

    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!

  • @masterroyjones4228
    @masterroyjones42282 жыл бұрын

    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

    @utkarshsharma6650

    2 жыл бұрын

    did you get placed?

  • @namangupta414

    @namangupta414

    Жыл бұрын

    No reply it means it's fake😂

  • @codingalley6229

    @codingalley6229

    6 ай бұрын

    bhai sabko notification nahi jata, bc kitne negative ho@@namangupta414

  • @jayadubey_22
    @jayadubey_223 жыл бұрын

    Congratulations for 100K subscribers takeUforward truly deserve this and more 🙌

  • @sagnikmitra710
    @sagnikmitra7103 жыл бұрын

    Next level explanation dada

  • @gauravm1253
    @gauravm12532 жыл бұрын

    So much energy for teaching bro. Amazing

  • @akanshapanchal7107
    @akanshapanchal71072 жыл бұрын

    What a nice way of explanation! More power to you :)

  • @sanatbhalla8537
    @sanatbhalla853711 ай бұрын

    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.

  • @nithishd6420
    @nithishd64203 жыл бұрын

    The enthusiasm was on different level💯

  • @jeet-smokey
    @jeet-smokey Жыл бұрын

    One of the finest of explanation. Kudos to you for your efforts.

  • @sakshamkapoor5905
    @sakshamkapoor59053 жыл бұрын

    Thank you for this, beautifully explained ❤

  • @aakashyadav6228
    @aakashyadav62283 жыл бұрын

    Congratulations on 100k bhaiya !!

  • @ayushbisht2689
    @ayushbisht26893 жыл бұрын

    Your energy 💯.

  • @sugun6875
    @sugun68754 ай бұрын

    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.

  • @jeevan-23
    @jeevan-235 ай бұрын

    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❤❤

  • @sayanganguly4806
    @sayanganguly4806 Жыл бұрын

    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.

    @programming_with_niraj.

    Жыл бұрын

    delete(resnode) should be commented

  • @paragroy5359
    @paragroy53592 жыл бұрын

    Nice explanation your videos are really good...please keep on making such videos.

  • @shuvbhowmickbestin
    @shuvbhowmickbestin Жыл бұрын

    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.

  • @ayushshukla2103
    @ayushshukla21032 жыл бұрын

    Amazing explanation. Thank you.

  • @bhaveshkumar6842
    @bhaveshkumar68422 жыл бұрын

    You explain soooo well!

  • @shreyasingh542
    @shreyasingh5423 жыл бұрын

    "This guy" Best explaination one could find over web!

  • @viswanathank2551
    @viswanathank25513 жыл бұрын

    Love u 3000 man best content for programming in earth !

  • @ankursharma6084
    @ankursharma60843 жыл бұрын

    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.

  • @original_gangsta_
    @original_gangsta_2 жыл бұрын

    OP explanation and energy 👌👌👌🔥🔥🔥

  • @jayeshsuthar5590
    @jayeshsuthar5590 Жыл бұрын

    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)

  • @jayeshl5968
    @jayeshl59683 жыл бұрын

    perfect explanation broo...ur doing great

  • @swapnanilgupta3046
    @swapnanilgupta30463 жыл бұрын

    Such a great explanation

  • @kbhargavi4400
    @kbhargavi44002 жыл бұрын

    truly awesome explaination !! : ) Thank you bro !!

  • @yogeshedekar6078
    @yogeshedekar60786 ай бұрын

    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 ?

  • @baap6382
    @baap63822 жыл бұрын

    You are a saviour. You achieve all the success in the world

  • @deyanpetrov4447
    @deyanpetrov4447 Жыл бұрын

    Best explanation I've seen yet, earned yourself a sub

  • @harshalgarg1149
    @harshalgarg11493 жыл бұрын

    Amazing explanation.

  • @piyusht20
    @piyusht202 жыл бұрын

    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

    @bikrantjajware6062

    2 жыл бұрын

    same doubt , i think its not necessary to delete the node

  • @dhanesh.s

    @dhanesh.s

    2 жыл бұрын

    It will take O(n) time to search the node Right?

  • @adityabalodi1185

    @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.

  • @kushwanthkapa2041
    @kushwanthkapa2041 Жыл бұрын

    We can directly store the value in hash map instead of DLL then also it is same right?

  • @m.vineeth9724
    @m.vineeth97243 жыл бұрын

    Awesome explanation 💯💯💯

  • @webdesignersandip7469
    @webdesignersandip74697 ай бұрын

    Really appreciate your passion you put in explaining....

  • @rajeshg3570
    @rajeshg35702 жыл бұрын

    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

    @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

    @rajeshg3570

    Жыл бұрын

    @@Saurabh-lb1uv thanks for the clarification

  • @igaming_
    @igaming_2 жыл бұрын

    Thanks for your Efforts

  • @therealg9542
    @therealg9542 Жыл бұрын

    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!

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 Жыл бұрын

    Love the way u explain😍😍

  • @stith_pragya
    @stith_pragya3 ай бұрын

    UNDERSTOOD.............Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @karthiksharma8720
    @karthiksharma87202 жыл бұрын

    such a great explanation bro

  • @codespace747
    @codespace7473 жыл бұрын

    Your videos is awesome♥️

  • @vipsclassicalboy
    @vipsclassicalboy2 жыл бұрын

    your energy is the awesome, keep it up bro..

  • @NAVEENKUMAR-ch4gf
    @NAVEENKUMAR-ch4gf3 жыл бұрын

    amazing explanation!!!!!

  • @AlbertoRodriguez-oe6jo
    @AlbertoRodriguez-oe6jo2 жыл бұрын

    Brilliant explanation.

  • @shivammehta9661
    @shivammehta96613 жыл бұрын

    Very Nice Explanation....Keep making videos

  • @ManishaKumari-lp4kw
    @ManishaKumari-lp4kw2 жыл бұрын

    Explanation and Energy !!!!💯🔥

  • @deepikasahu1592
    @deepikasahu1592 Жыл бұрын

    Best explanation so far🙌

  • @adityaathiratha6104
    @adityaathiratha61043 жыл бұрын

    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.

  • @sarveshsawant7232
    @sarveshsawant7232 Жыл бұрын

    Great video, one question can't we implement the same with singly linked list?

  • @varunsingh6480
    @varunsingh64802 жыл бұрын

    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

    @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

  • @amitagrawal4660
    @amitagrawal46603 жыл бұрын

    very nicely explained :)

  • @muskangupta5873
    @muskangupta58733 жыл бұрын

    big thanks bhaiya.. such a nice explanation🤩

  • @divyam69
    @divyam69 Жыл бұрын

    One of the best explanation

  • @Anurag-kp6wc
    @Anurag-kp6wc2 жыл бұрын

    Thanks Bhaiya! You're the best :D

  • @loveleshsharma5663
    @loveleshsharma56632 жыл бұрын

    Good Explanation!

  • @shimul6680
    @shimul66802 жыл бұрын

    You are a great genius....

  • @adityajain1205
    @adityajain12053 жыл бұрын

    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

    @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

    @mohdhammadsiddiqui7598

    2 жыл бұрын

    @@TheAnandsingh111 i think using hashmap we can find , queue + hasmap will it work?

  • @TheAnandsingh111

    @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

    @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

    @TheAnandsingh111

    2 жыл бұрын

    @@mohdhammadsiddiqui7598 cool 🙌

  • @raj_kundalia
    @raj_kundalia Жыл бұрын

    thanks for the explanation!

  • @nexonsensei3793
    @nexonsensei37933 ай бұрын

    Really great explanation👏

  • @kunalverma9777
    @kunalverma97773 жыл бұрын

    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

    @takeUforward

    3 жыл бұрын

    Yes you can improve that point for sure.

  • @takeUforward

    @takeUforward

    3 жыл бұрын

    Yes you can improve that point for sure.

  • @kunalverma9777

    @kunalverma9777

    3 жыл бұрын

    @@takeUforward Thankyou bhaiya

  • @anonymousvoid6356
    @anonymousvoid63562 жыл бұрын

    striver is on fire in this video. 🔥🔥🔥🔥

  • @aasthajha1051
    @aasthajha10513 жыл бұрын

    awesome...................

  • @auroshisray9140
    @auroshisray91402 жыл бұрын

    Nicely explained

  • @wisdomkhan
    @wisdomkhan2 жыл бұрын

    Why was this given in Stack and Queues section in SDE sheet?

  • @saurabhtrigunayat4071
    @saurabhtrigunayat4071 Жыл бұрын

    we can also solve this question easily using combination of (HashMap+LinkedHashMap) in java.

  • @amitarya4894
    @amitarya48942 жыл бұрын

    Great bhai..🙏🙏

  • @sanchitjain9745
    @sanchitjain97452 жыл бұрын

    Great Explanation.... :)

  • @aryansinha1818
    @aryansinha18182 жыл бұрын

    *Thank You so much. Love & Respect always.*

  • @piyushkumar2437
    @piyushkumar2437 Жыл бұрын

    Why you have used Doubly LinkedList. Why not use singly linked list and maintain pointers pointing to start and rear. Please advise.

  • @shivam9048
    @shivam9048 Жыл бұрын

    Very well understood

  • @Anoyashu
    @Anoyashu2 жыл бұрын

    Amazing !!!

  • @sivrajsubba94
    @sivrajsubba94 Жыл бұрын

    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

    @rohanmahto3123

    Жыл бұрын

    you just change the position of the node after the head as it is the recently used

  • @rohandsouza9147
    @rohandsouza91472 жыл бұрын

    Cant be thankful enough man !!

  • @SurajGaud
    @SurajGaud3 жыл бұрын

    Well explained

  • @devanshmesson2777
    @devanshmesson27772 жыл бұрын

    Thank you so much striver!

  • @shubhambhadra5370
    @shubhambhadra53705 ай бұрын

    Awesome explanation ❤❤

  • @AbhinavSrivastava-xr5jz
    @AbhinavSrivastava-xr5jz Жыл бұрын

    the finesse of a Google engineer 😍🤩

  • @neerajgupta3942
    @neerajgupta39426 ай бұрын

    why create a new address in case of node shifting. Only the previous and next can be updated?

  • @SahilAhuja-hy9gg
    @SahilAhuja-hy9gg Жыл бұрын

    best explanation this ques could ever have

  • @mattdamon1667
    @mattdamon16673 жыл бұрын

    Merci Merci

  • @vishnugandlur3875
    @vishnugandlur3875 Жыл бұрын

    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

    @a_05_himanshu_akula31

    Жыл бұрын

    Access by key ..dont search

  • @akashchaudhury3240
    @akashchaudhury32402 ай бұрын

    Nice explanation 🔥

  • @dheerajmaurya3418
    @dheerajmaurya34182 жыл бұрын

    Can I use queue data structure instead of DLL??

  • @pankajarora8263

    @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.

  • @vaibhavagarwal1479
    @vaibhavagarwal14792 ай бұрын

    Why is this question marked as a stack/queue question in your sheet?? neither stack nor queue is used?

  • @darkstudio3170
    @darkstudio3170 Жыл бұрын

    10:19 "it does" hit me hard

  • @KCODivyanshuKatyan
    @KCODivyanshuKatyan2 жыл бұрын

    brilliant as usual

  • @ashwaniagrawal27
    @ashwaniagrawal27Ай бұрын

    anyone tell me how i can get itution that the problem can be solved in this way

  • @user-tx3mo1ez2n
    @user-tx3mo1ez2n3 жыл бұрын

    from functools import lru_cache But well explained 👍👍

  • @takeUforward

    @takeUforward

    3 жыл бұрын

    Not allowed in interview

  • @user-tx3mo1ez2n

    @user-tx3mo1ez2n

    3 жыл бұрын

    @@takeUforward ha bhaiya.