L18. Implement LRU Cache

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ก.พ. 2025
  • Find problem link, notes under Step 9: takeuforward.o...
    Follow me on socials: linktr.ee/take...

ความคิดเห็น • 116

  • @HMBDM1976
    @HMBDM1976 6 หลายเดือนก่อน +56

    Hi Striver,
    I just wanted to thank you for your incredible DSA tutorials. Your clear explanations and thorough examples have made a huge difference in my understanding and proficiency. Thanks to your videos, I've become very proficient in DSA, which has significantly boosted my confidence and performance in my coding
    Keep up the great work, you're making a big difference!

    • @abhimanyus8757
      @abhimanyus8757 6 หลายเดือนก่อน

      how it, am started today, did u make notes?

    • @omkarshendge5438
      @omkarshendge5438 5 หลายเดือนก่อน

      @@abhimanyus8757 notes is compulsory

  • @harshitaninama5235
    @harshitaninama5235 6 หลายเดือนก่อน +13

    Finally the stack playlist is here!!! thankyou so much!

  • @yashsharma6108
    @yashsharma6108 6 หลายเดือนก่อน +20

    You are such a hardworking fellow .... seriously you inspire me everyday to work even harder to make myself better day by day...thanku striver😊

    • @DevilJim-p1s
      @DevilJim-p1s 5 หลายเดือนก่อน

      Bro I'm just looking for a review I've just started are you good enough now at the end?

    • @CodewithKing360
      @CodewithKing360 2 หลายเดือนก่อน

      Bhai kitna din lagega is karne me

  • @karthik-varma-1579
    @karthik-varma-1579 4 หลายเดือนก่อน +5

    I Have Learned this Technique in my Operating Systems Subject in Page Replacement Algorithms using Stack
    And Now I am in My 3-1 Semester Again visiting to the LRU cache in Data Structures seems the main use of it and
    Striver Has Very Well Explained it.
    And Now I am able to Do My own for LFU Cache.
    Thank God!!!

  • @agrawalmitesh4395
    @agrawalmitesh4395 6 หลายเดือนก่อน +1

    Gem , just saw your explanation of first 13 min how to do it , and was able to implement it myself , in very less time

  • @akesh500
    @akesh500 6 หลายเดือนก่อน +17

    Please consider doing a series on Strings.

  • @_ankit_gupta_
    @_ankit_gupta_ 6 หลายเดือนก่อน +9

    just going to start older stack playlist
    but got new playlist
    thanks you striver !!!

  • @THOSHI-cn6hg
    @THOSHI-cn6hg 2 หลายเดือนก่อน +4

    he has started taking yt channel for granted

  • @Sumantashee-e3w
    @Sumantashee-e3w 6 หลายเดือนก่อน +2

    Sir you are a good teacher in DSA.... I'm injoy this playlist
    full stack playlist please 🙏🙏

  • @aryanbundela6031
    @aryanbundela6031 6 หลายเดือนก่อน +8

    My goat grinding hard at night

  • @prithvirathore7436
    @prithvirathore7436 6 หลายเดือนก่อน +15

    Hello ,Raj sir I hope this comment finds you well,as String is the most important topic in the dsa when it comes to placements,so it's a humble request from you to please make videos on strings .as my placements are going on and arrays and strings are much asked questions

  • @user2k56
    @user2k56 4 หลายเดือนก่อน +6

    @takeUforward We need a string series brother plz , as soon as possible.#A2Z DSA

  • @mohammedrayan2192
    @mohammedrayan2192 6 หลายเดือนก่อน +2

    Hello raj bhaiya. Thank very much for taking your valuable and effort to teach DSA. Please create a video on Java Collection Frameworks.

  • @kunal5077
    @kunal5077 3 หลายเดือนก่อน

    Very Nice explanation. Eagerly waiting for strings playlist

  • @harigs72
    @harigs72 3 หลายเดือนก่อน +2

    Oh my goodness! I found the optimal solution of it by myself. I have just come here to see the most optimal solution of it..😅

  • @abhiroopmokshagnabheemineni
    @abhiroopmokshagnabheemineni 4 หลายเดือนก่อน

    Such a beautiful explanation!! Kudos!

  • @HEMANTPORWAL-t7d
    @HEMANTPORWAL-t7d 6 หลายเดือนก่อน

    thank you striver for all these interesting stuffs

  • @swathigp3818
    @swathigp3818 28 วันที่ผ่านมา

    Incredible explanation

  • @ishan9604
    @ishan9604 6 หลายเดือนก่อน +1

    Thank You Striver , you really help me in trees and graph series!

  • @HemangSinghal-op5ep
    @HemangSinghal-op5ep 5 หลายเดือนก่อน

    Wonderful Explanation

  • @omkarshendge5438
    @omkarshendge5438 6 หลายเดือนก่อน +18

    currafterhead→prev = node; this step is missing in the insertafterhead function

    • @AbhinavMohanParaverse
      @AbhinavMohanParaverse 3 หลายเดือนก่อน +2

      Wanted to make a comment to point this out, but you got it!

  • @KeigoEdits
    @KeigoEdits 6 หลายเดือนก่อน +3

    I respect you a lot striver literally but please take this suggestion into consideration, dont make videos for the topics you already made earlier
    We are eagerly waiting for strings series and recursion series

  • @safionweb
    @safionweb 8 วันที่ผ่านมา

    Wonderful

  • @ShivamGupta-ei2sv
    @ShivamGupta-ei2sv 3 หลายเดือนก่อน +1

    Bhaiyya I am eagerly waiting for string playlist

  • @Panther_jf
    @Panther_jf 6 หลายเดือนก่อน

    Bhaiya, would love to learn Binary Trees next, pls❤
    And the stack playlist was amazing as always😍

    • @KeigoEdits
      @KeigoEdits 6 หลายเดือนก่อน

      Binary trees playlist is already completed, go check out

    • @Panther_jf
      @Panther_jf 6 หลายเดือนก่อน

      @@KeigoEdits I was talking about the new version of binary trees playlist

  • @Hy_there01
    @Hy_there01 4 หลายเดือนก่อน +2

    This video lasts longer than my relationship 😂

    • @magha786
      @magha786 4 วันที่ผ่านมา

      Nice 😂

  • @ParthDabhiOfficial
    @ParthDabhiOfficial หลายเดือนก่อน

    UNDERSTOOD, Radha Radha

  • @curs3m4rk
    @curs3m4rk 5 หลายเดือนก่อน

    I was able to implement after watching approach of DLL and HashMap,
    Got stuck at the place where I was able to delete LRU from DLL, but could not find out how to remove that node from hashmap.
    Storing key, value pair in DLL Node is a great idea.
    Thanks :)

  • @AbhijeetChouhanXD
    @AbhijeetChouhanXD 3 หลายเดือนก่อน +2

    strings ke videos le aao bhai

  • @swaroopparida0429
    @swaroopparida0429 6 หลายเดือนก่อน +5

    Hi striver please bring playlist on string

  • @shresthgupta2670
    @shresthgupta2670 6 หลายเดือนก่อน

    legend is here

  • @devanshimittal7986
    @devanshimittal7986 4 วันที่ผ่านมา

    Please include dequeue questions also in SDE Sheet

  • @GuptaHarshHemant
    @GuptaHarshHemant 6 หลายเดือนก่อน

    thanks striver

  • @iamnottech8918
    @iamnottech8918 6 หลายเดือนก่อน

    mojj krdi bhaiya

  • @KedarBhawthankar
    @KedarBhawthankar 5 หลายเดือนก่อน

    striver is legend

  • @Jai_Shri_Krishna_Shri_radhe
    @Jai_Shri_Krishna_Shri_radhe 6 หลายเดือนก่อน +10

    If you Reached 13:35 skip to 20:45
    Again restarted writing pseudocode

    • @akhilesh_ku
      @akhilesh_ku 6 หลายเดือนก่อน +2

      No , firstly he just wrote pseudo code , for second time it's cpp code😅😂

    • @omkarshendge5438
      @omkarshendge5438 6 หลายเดือนก่อน

      yup exactly

    • @omkarshendge5438
      @omkarshendge5438 6 หลายเดือนก่อน

      @@akhilesh_ku the first time also it is cpp heavy only bro.

  • @umeshkaushik710
    @umeshkaushik710 4 หลายเดือนก่อน +1

    Hi Bhaiya, Please complete Strings list from the sheet, and I think Strings question might need some more addition.
    When we can except String Playlist out ?

  • @DeadPoolx1712
    @DeadPoolx1712 4 หลายเดือนก่อน

    UNDERSTOOD;

  • @updownftw
    @updownftw หลายเดือนก่อน +3

    Why is this in the Stack and queue playlist ?

  • @astitvashukla6556
    @astitvashukla6556 6 หลายเดือนก่อน

    Bhaiya I am following your DSA series ek baar Java collection kie video daal dijiye bahut help ho jaaegi and bhaiya meine kaafi se question practice kre solve kre but confidence and logic dono build nhi ho paa rha... Need some guidance

  • @kanishasiva6731
    @kanishasiva6731 6 หลายเดือนก่อน

    Striver 💯🙏💗

  • @AbhishekKumar-td5zu
    @AbhishekKumar-td5zu หลายเดือนก่อน

    understood!!

  • @venumsingh
    @venumsingh 6 หลายเดือนก่อน

    Thanks for the video. Which software is used for recording the video?

  • @deveshagarwal660
    @deveshagarwal660 5 หลายเดือนก่อน +1

    You mentioned that you will be using a DLL for solving this, but never explained that how did you come to a conclusion of using a DLL.
    It is also important to explain your choice of using any particular data structure or algorithm when someone is solving problems in an interview.

    • @AbhijeetChouhanXD
      @AbhijeetChouhanXD 3 หลายเดือนก่อน +1

      Because we are (i) adding elements to front (ii) removing from the back and adding to front
      In say singly LL, to remove the last element, we would have to travel the list taking O(n) time. Similarly in something like an array, to add an element to the front we would have to shift all the elements, taking O(n) time.

  • @madhupathak7772
    @madhupathak7772 6 หลายเดือนก่อน +1

    Hello sir!! Please upload the heaps playlist asap

  • @prashlovessamosa
    @prashlovessamosa 6 หลายเดือนก่อน

    Sir can you please add the solutions in python too
    I have to visit the third party website to read the code.
    Very grateful to you for the series.

  • @arunbhagat8023
    @arunbhagat8023 หลายเดือนก่อน +1

    Sir String and Heap Playlist Please....!!!

  • @adhiraj2964
    @adhiraj2964 3 หลายเดือนก่อน

    Striver bhaiya next topic string cover kariye pleaseee

  • @abhinavabhi3568
    @abhinavabhi3568 หลายเดือนก่อน

    Understood

  • @souvikbasak4396
    @souvikbasak4396 6 หลายเดือนก่อน +1

    Bhaiya could you tell beforehand if you going upload any chapter(specsilly graph and dp) playlist which is already available in older playlist , so that instead of the older playlist I could wait a few more days for the newer playlist , like are you going to upload new playlist of graphs and dp , else i will continue with the older one only.

  • @atulwadhwa192
    @atulwadhwa192 3 หลายเดือนก่อน

    My approach using dummy node
    class Node{
    public:
    int key,val;
    Node* next;
    Node* prev;
    Node(int key,int val){
    this->key = key;
    this->val = val;
    next = NULL;
    prev = NULL;
    }
    };
    class LRUCache {
    int size,capacity;
    Node* head;
    Node* tail;
    unordered_map mp;
    public:
    LRUCache(int capacity) {
    size =0;
    this->capacity = capacity;
    head = new Node(-1,-1);
    tail = new Node(-1,-1);
    head->next = tail;
    tail->prev = head;
    }

    void insertNodeAtEnd(Node* node){
    // detachment of node
    Node* prevNode = node->prev;
    Node* nextNode = node->next;
    if(prevNode != NULL) prevNode->next = nextNode; // for new node prevNode would be null
    if(nextNode != NULL) nextNode->prev = prevNode; // for new node nextNode would be null

    // attachment of node at end
    Node* tailPrev = tail->prev;
    tailPrev->next = node;
    node->prev = tailPrev;
    node->next = tail;
    tail->prev = node;
    }

    int get(int key) {
    uj7i8if(mp.find(key) == mp.end())
    return -1;

    Node* node = mp[key];
    insertNodeAtEnd(node);
    return node->val;
    }

    void put(int key, int value) {
    if(mp.find(key)!=mp.end()){
    Node* node = mp[key];
    node->val = value;
    insertNodeAtEnd(node);
    }
    else{
    Node* node = NULL;
    if(size == capacity){
    node = head->next;
    mp.erase(node->key);
    node->key = key;
    node->val = value;
    // insertNodeAtEnd(node);
    }
    else{
    size++;
    node = new Node(key,value);
    // insertNodeAtEnd(node);
    }
    insertNodeAtEnd(node);
    mp[key] = node;
    }
    }
    };
    /**
    * 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);
    */

  • @THOSHI-cn6hg
    @THOSHI-cn6hg 4 หลายเดือนก่อน +1

    upload next series cmmon bro

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 6 หลายเดือนก่อน

    thanks sir

  • @FarziWada-q3r
    @FarziWada-q3r 2 หลายเดือนก่อน +2

    The last video of a2z sheet was added 4 months ago. When will the next videos be uploaded?

  • @ShubhamKumar-vk8me
    @ShubhamKumar-vk8me 5 หลายเดือนก่อน +1

    Bhaiya String ka video kab tak aajaye, You said you will be uploding it before september

  • @sanchitkaul5084
    @sanchitkaul5084 24 วันที่ผ่านมา

    Rather than "DeleteNode" a better name of the function would be "DetachNode" .

  • @impatientgaming9868
    @impatientgaming9868 หลายเดือนก่อน

    niceee

  • @karthi2174
    @karthi2174 2 หลายเดือนก่อน +2

    Hello everyone, my placements are starting in March 2025. I have started learning DSA using the TakeUforward SDE sheet and have completed the first 10 problems. However, I was able to understand only 3 of them, and I am struggling to build logic on my own. I am unsure if I am on the right track or where to start. Could you please give me some advice on how to approach solving problems independently and where I should begin? I have programming knowledge but need guidance. I am not aiming to join a big MNC company, but I want to prepare effectively.

    • @AbhinavTries
      @AbhinavTries หลายเดือนก่อน

      youre doing just fine bro, you've just started, i've been there, im done with 50% of the sheet yet I struggle with intuition, dont worry just keep practicing
      comment your own notes in the code, it'll help in revising
      also whenever you approach a new approach, write it down somewhere

  • @rameezalipacific
    @rameezalipacific 6 หลายเดือนก่อน

    I'm from Pakistan and currently pursuing a degree in software engineering. Alongside my studies, I'm dedicating significant time to mastering data structures and algorithms (DSA). However, I'm uncertain if excelling in DSA alone is enough to secure a job at top tech companies like the one you work for. Do I need to develop additional skills, such as web, app, or game development, to increase my chances of success? Also, could you share your experience on when you started focusing on DSA-was it during your degree or afterward?

  • @FarziWada-q3r
    @FarziWada-q3r 2 หลายเดือนก่อน +1

    When will be the next video uploaded?

  • @abhishekagrawal3730
    @abhishekagrawal3730 6 หลายเดือนก่อน +4

    Bhaiya please string dal do

  • @atulwadhwa192
    @atulwadhwa192 3 หลายเดือนก่อน

    Will there be any difference if we delete from front and insert at the back? In terms of complexity?

  • @mayanksaini4827
    @mayanksaini4827 6 หลายเดือนก่อน

    kya haal h striver bhai k👀👀 ?

  • @AneeshPal-t7y
    @AneeshPal-t7y 2 หลายเดือนก่อน

    I m just not able to get that when we change order of elements so that it becomes the most recently used , why we are not changing the reference i.e. new location in map as now nodes location is changed....

  • @theultimatespo...4205
    @theultimatespo...4205 หลายเดือนก่อน

    Can it be done by using dequeue

  • @Aman-Gairola
    @Aman-Gairola 5 หลายเดือนก่อน

    he forgot change afterhead->prev = node in insert funciton

  • @MO-fg2cm
    @MO-fg2cm 2 หลายเดือนก่อน

    Lru in processors done in maps ?? No way , is this some dsa course then ?

  • @arl0817osho
    @arl0817osho 6 หลายเดือนก่อน

    does it should be afterhead->prev = node in insert funciton also?

  • @NirajPatel-ug2ew
    @NirajPatel-ug2ew 3 หลายเดือนก่อน +1

    my code:
    struct Node {
    int val;
    int key;
    struct Node* next;
    struct Node* prev;
    Node(int value, int x) {
    val = value;
    key = x;
    next = NULL;
    prev = NULL;
    }
    };
    class LRUCache {
    public:
    unordered_map m;
    int size, currsize;
    struct Node* head;
    struct Node* tail;
    LRUCache(int capacity) {
    size = capacity;
    head = NULL;
    tail = NULL;
    currsize = 0;
    }
    int get(int key) {
    if (m[key] == NULL)
    return -1;
    struct Node* temp = m[key];
    if (temp == head) {
    return temp->val;
    }
    if(temp == tail){
    tail = temp->prev;
    }
    temp->prev->next = temp->next;
    if (temp->next != NULL) {
    temp->next->prev = temp->prev;
    }
    temp->next = head;
    head->prev = temp;
    head = temp;
    return head->val;
    }
    void put(int key, int value) {
    if (m[key] == NULL) {
    if (currsize < size) {
    struct Node *temp = new Node(value, key);
    m[key] = temp;
    currsize ++;
    if(head == NULL){
    tail = temp;
    head = temp;
    return;
    }
    temp->next = head;
    head->prev = temp;
    head = temp;
    return ;
    } else {
    struct Node *temp = new Node(value, key);
    m[tail->key] = NULL;
    if(head == tail){
    tail = temp;
    head = temp;
    m[key] = head;
    return ;
    }
    tail = tail->prev;
    temp->next = head;
    head->prev = temp;
    head = temp;
    head->val = value;
    m[key] = head;
    return ;
    }
    } else {
    struct Node* temp = m[key];
    if (temp == head) {
    temp->val = value;
    return;
    }
    if(temp == tail){
    tail = temp->prev;
    }
    temp->prev->next = temp->next;
    if (temp->next != NULL) {
    temp->next->prev = temp->prev;
    }
    temp->next = head;
    head->prev = temp;
    temp->val = value;
    head = temp;
    return ;
    }
    return ;
    }
    };
    /**
    * 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);
    */

  • @ahmadrasheed2598
    @ahmadrasheed2598 หลายเดือนก่อน

    C++ Implentation:
    class Node {
    public:
    int key;
    int val;
    Node* prev;
    Node* next;
    Node(int key, int val) : key(key), val(val), prev(nullptr), next(nullptr) {}
    };
    class LRUCache {
    private:
    void deleteNode(Node* node){
    if (node == nullptr) {
    return;
    }
    node->prev->next = node->next;
    node->next->prev = node->prev;
    }
    void insertAtHead(Node* node){
    if (node == nullptr) {
    return;
    }
    node->next = head->next;
    head->next = node;
    node->prev = head;
    node->next->prev = node;
    }
    Node* head = new Node(-1, -1);
    Node* tail = new Node(-1, -1);
    public:
    int capacity = 0;
    unordered_map mp;
    LRUCache(int capacity) {
    head->next = tail;
    tail->prev = head;
    this->capacity = capacity;
    mp.clear();
    }
    int get(int key) {
    if(mp.find(key) != mp.end()){
    // key exists
    Node* node = mp[key];
    deleteNode(node);
    insertAtHead(node);
    return node->val;
    } else{
    // key doesnt exist
    return -1;
    }
    }
    void put(int key, int value) {
    if(mp.find(key) != mp.end()){
    // key exists update
    Node* node = mp[key];
    node->val = value;
    deleteNode(node);
    insertAtHead(node);
    } else{
    Node* node = new Node(key, value);
    // key doesnt exist add
    if(mp.size() == capacity){
    //remove least recent
    Node* lru = tail->prev;
    deleteNode(lru);
    mp.erase(lru->key);
    insertAtHead(node);
    mp[key] = head -> next;
    } else{
    //add
    insertAtHead(node);
    mp[key] = head -> next;
    }
    }
    }
    };

  • @DrawwithNavi
    @DrawwithNavi 6 หลายเดือนก่อน

    cant we just do the same thing using orderedDict, will take similar time and much shorter

  • @Leo-id4uh
    @Leo-id4uh 6 หลายเดือนก่อน

    code 360 not showing some of the code in the solved list which i was done before, can anyone tell about this

  • @sandeepxt99
    @sandeepxt99 6 หลายเดือนก่อน

  • @soumyadey4500
    @soumyadey4500 5 หลายเดือนก่อน

    can anyone help why should i use "m[key_] = head->next" under get function?

  • @odorlessflavorless
    @odorlessflavorless 4 หลายเดือนก่อน

    ~ 17:54 'we have touched someone, ... least recently used' --- we have to put in front because it is the most recently used and not the least recently used.

  • @SanketPatil-gm7pb
    @SanketPatil-gm7pb 6 หลายเดือนก่อน +3

    sir plz upload the next playlist of heap

  • @user-zp1dv4yh5e
    @user-zp1dv4yh5e 4 หลายเดือนก่อน

    Here, you didn't handle the case where the key could be same be value might be different.

  • @yogeshwardayalagarwal4228
    @yogeshwardayalagarwal4228 6 หลายเดือนก่อน +1

    Why is it not a part of linked list playlist?

    • @akhilesh_ku
      @akhilesh_ku 6 หลายเดือนก่อน

      It uses put and get

  • @SoRandominfo
    @SoRandominfo 6 หลายเดือนก่อน +17

    U missed LFU cache video?

    • @brokegod5871
      @brokegod5871 6 หลายเดือนก่อน +4

      He solved it before in an older video, you can see in the playlist

    • @akhilesh_ku
      @akhilesh_ku 6 หลายเดือนก่อน +8

      See down 😅 , TH-cam suggesting you

    • @maverick_8707
      @maverick_8707 5 หลายเดือนก่อน +1

      He has included in the playlist at last

    • @Tan_cannon
      @Tan_cannon 15 วันที่ผ่านมา +1

      ​@@akhilesh_ku😂😂

  • @vineet1728
    @vineet1728 6 หลายเดือนก่อน

    The editor forgot something to clip

  • @nikhil_squats
    @nikhil_squats 6 หลายเดือนก่อน

    curr_after_head->prev=node; in addAfterHead Func

  • @shantipriya370
    @shantipriya370 6 หลายเดือนก่อน

    can you pls give python code link..

  • @FreeFire-f7e
    @FreeFire-f7e 6 หลายเดือนก่อน

    Hello sir I will go in IIT ropar what language which I do start?

  • @UttamSingh-vn1yf
    @UttamSingh-vn1yf 3 หลายเดือนก่อน +1

    string krwado

  • @razorzeast4133
    @razorzeast4133 6 หลายเดือนก่อน +1

    I'm the first one

  • @ishan9604
    @ishan9604 6 หลายเดือนก่อน +1

    First Comment

  • @SajanKumar-ec2us
    @SajanKumar-ec2us 6 หลายเดือนก่อน

    please explain c++ written don't write suedo code

    • @studystuff51
      @studystuff51 6 หลายเดือนก่อน +4

      Please learn how to write code by yourself instead

  • @chiragdusad2015
    @chiragdusad2015 5 หลายเดือนก่อน

    Stiver where are your hairs going😢
    Looks like our brother is getting older

  • @vishal5850
    @vishal5850 14 วันที่ผ่านมา

    How about using linked hash map?
    this works!!
    class LRUCache {
    private final int capacity;
    private final LinkedHashMap map;
    public LRUCache(int cap) {
    this.capacity = cap;
    this.map = new LinkedHashMap();
    }
    public int get(int key) {
    if (map.containsKey(key)) {
    int value = map.remove(key);
    map.put(key, value);
    return value;
    }
    return -1;
    }
    public void put(int key, int value) {
    if (map.containsKey(key)) {
    map.remove(key);
    } else if (map.size() >= capacity) {
    Integer eldestKey = map.keySet().iterator().next();
    map.remove(eldestKey);
    }
    map.put(key, value);
    }
    }