LRU cache | Live Coding with Explanation | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

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

  • @AlgorithmsMadeEasy
    @AlgorithmsMadeEasy  3 ปีที่แล้ว

    We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!
    Questions you might like:
    ✅✅✅[ Tree Data Structure ] : th-cam.com/play/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls.html
    ✅✅✅[ Graphs Data Structure ] : th-cam.com/play/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C.html
    ✅✅✅[ January Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn.html
    ✅✅✅[ December Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA.html
    ✅✅✅[ November Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e.html
    ✅✅✅[ August Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe.html
    ✅✅✅July Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC-.html
    ✅✅✅June Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG.html
    ✅✅✅May Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e.html
    ✅✅✅Cracking the Coding Interview - Unique String: th-cam.com/play/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE.html
    Struggling in a question??
    Leave in a comment and we will make a video!!!🙂🙂🙂

  • @shivendra2076
    @shivendra2076 2 ปีที่แล้ว +4

    Under rated channel, Thanks bro, understood it

    • @AlgorithmsMadeEasy
      @AlgorithmsMadeEasy  2 ปีที่แล้ว

      Thank you!!
      Do not forget to subscribe to the channel!! 😊😊

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

    one of the best solution...

  • @dambar67
    @dambar67 2 ปีที่แล้ว

    map.remove(node), how it will remove ,here key should be given not the value.

  • @appprocessorsappdevelopmen8405
    @appprocessorsappdevelopmen8405 3 ปีที่แล้ว

    *insert() method without declaring headNext*
    void insert(Node node){
    //insert into map
    map.put(node.key,node);
    //point node's next to head's next
    node.next = head.next;
    //point head's next' prev to node
    head.next.prev = node;
    head.next=node;
    node.prev=head;
    }

  • @milenitrivedi7561
    @milenitrivedi7561 2 ปีที่แล้ว

    Great explanation !

  • @RupamSasmalYt
    @RupamSasmalYt 2 ปีที่แล้ว

    Have done the same approach with c++, but it's giving TLE. Can't find any single solution in c++ without tle in leetcode :(
    C++ Code:
    class Node{
    public:
    int key, val;
    Node *prev, *next;
    Node(int key,int val){
    this->key=key;
    this->val=val;
    }
    };
    class LRUCache {
    public:
    int cap;
    Node* head=new Node(0,0);
    Node* tail=new Node(0,0);
    unordered_map mp;

    LRUCache(int capacity) {
    cap=capacity;
    head->next=tail;
    tail->prev=head;
    }
    void Add(Node* newNode){
    mp[newNode->key]=newNode;
    Node* tmp=head->next;
    head->next=newNode;
    newNode->prev=head;
    newNode->next=tmp;
    tmp->prev=newNode;
    }
    void Delete(Node* node){
    mp.erase(node->key);
    node->prev->next=node->next;
    node->next->prev=node->prev;
    }
    int get(int key) {
    if(mp.find(key)!=mp.end()){ // key present
    Node* resNode=mp[key];
    Delete(resNode);
    Add(resNode);
    return resNode->val;
    }
    return -1;
    }

    void put(int key, int value) {
    if(mp.find(key)!=mp.end()){ // key is present
    Delete(mp[key]);
    }
    if(mp.size()==cap){
    Delete(tail->prev);
    }
    Add(new Node(key,value));
    }
    };

  • @omarkhan5223
    @omarkhan5223 2 ปีที่แล้ว

    @5:40 You remove tail.prev. I thought you would remove tail because the tail of linked list is the last node, isn't tail.prev the second last node?? Why is this not the case?

    • @054_heenaahmed8
      @054_heenaahmed8 2 ปีที่แล้ว

      tail is the dummy node that is at end of linkedlist , similar to head , which is also dummy at start of linkedlist . we have used it to simplify the operations , and get access to last(using tail) and first(using head) node in O(1).

    • @rilkedev449
      @rilkedev449 2 ปีที่แล้ว +1

      @@054_heenaahmed8 Thank you.

    • @sayyidiskandarkhan3064
      @sayyidiskandarkhan3064 ปีที่แล้ว

      it would have been great had he covered this in the explaination but i had to look at the comments to find the results.

  • @CRamPrasannaCV
    @CRamPrasannaCV 3 ปีที่แล้ว

    Understood :-)