LRU cache | EP 22

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 74

  • @facttecher3039
    @facttecher3039 7 місяців тому +1

    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.

  • @noob_clasher511
    @noob_clasher511 3 роки тому +10

    done for this weekend, looking forward to next. Thank you sir.

  • @desihiphop4998
    @desihiphop4998 3 роки тому +2

    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 !!!🖤🖤

  • @ece116pranaykumar4
    @ece116pranaykumar4 2 роки тому +10

    getting TLE now

  • @ayushbhagwat3440
    @ayushbhagwat3440 2 роки тому +4

    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 роки тому +1

      so what u did?

    • @gantayat
      @gantayat 2 роки тому +2

      Keep submitting, it will pass randomly xD (shorturl.at/AU157 )

  • @Wanna_be_01
    @Wanna_be_01 2 роки тому +5

    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 :)

  • @mohdsaadalikhan7209
    @mohdsaadalikhan7209 3 роки тому +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?

  • @mgdesire9255
    @mgdesire9255 3 роки тому +3

    finally S2 done...looking forward Bhaiya can't wait for upcoming season.

  • @baap6382
    @baap6382 3 роки тому +11

    Sir your videos are absolute gold.
    Keep posting.
    I am in final year.
    Wished I was born 2-3 years later

  • @_inspireverse___
    @_inspireverse___ Рік тому +3

    Well explained. Thank you so much 👍

  • @krishnaradhey2814
    @krishnaradhey2814 Рік тому +1

    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

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

  • @ritulraj6119
    @ritulraj6119 Рік тому +1

    Great Explanation 👌

  • @vikassinghbisht7305
    @vikassinghbisht7305 9 місяців тому

    u r best fraz bhaiya

  • @abhayrai1724
    @abhayrai1724 2 роки тому +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 😍🤗

  • @HarshRaj-kp7gk
    @HarshRaj-kp7gk 3 роки тому +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 3 роки тому +1

      Gate Smashers se jaake pdle bhai aur saath ke saath notes likhlio

  • @louistomlinson3367
    @louistomlinson3367 2 роки тому +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;
    }
    }

  • @vivekmahindrakar2996
    @vivekmahindrakar2996 2 роки тому

    Can we use Stacks where the most recently used is the top and lru is at the bottom???

  • @lalit-singh-bisht
    @lalit-singh-bisht Рік тому +1

    i was not expecting a stl solution

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

  • @mdnadeem6343
    @mdnadeem6343 3 роки тому +1

    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.

  • @manishvijay6794
    @manishvijay6794 2 роки тому +1

    Awesome content 🔥🔥

  • @rishupatel7285
    @rishupatel7285 2 роки тому +3

    Is anyone getting TLE For this solution

  • @editorera239
    @editorera239 3 роки тому +1

    Thnx bhaiya
    In series m linked list complete ho jaega na overall I mean

  • @code7434
    @code7434 2 роки тому +5

    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

  • @harendrameghwal1120
    @harendrameghwal1120 2 роки тому +4

    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

  • @TekaneChaitanya
    @TekaneChaitanya 3 роки тому +2

    Please make videos using JAVA

    • @SunnyGupta00
      @SunnyGupta00 3 роки тому

      It is very similar to c++ (.) Ki jaga (->) ko replace kar do

  • @vasuoberoi1810
    @vasuoberoi1810 2 роки тому

    which whiteboard u are using?

  • @Sai-fp3tu
    @Sai-fp3tu 3 роки тому +1

    Overall linked list ka kitne videos ayenge bhaiya?
    Btw Awesome teaching 🔥🔥

  • @souravkumar5120
    @souravkumar5120 2 роки тому +3

    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 роки тому +1

      @@bitrish34 i am using unordered map and it is still giving me tle

  • @tech_wizard9315
    @tech_wizard9315 3 роки тому

    By when you will upload entire playlist

  • @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?

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

  • @deepakprajapat3897
    @deepakprajapat3897 3 роки тому

    Sir STL full concept ka uper aap video kab upload karoge
    Plzzzz

  • @guptashashwat
    @guptashashwat 2 роки тому +2

    G phat gyi karte karte but excellent explanation!

  • @mdnadeem6343
    @mdnadeem6343 3 роки тому

    zbrdst bhaiya

  • @MdFaisal-kj1ck
    @MdFaisal-kj1ck 3 роки тому

    Bhai linked list k baad STL please ASAP.🙏🙏

  • @anupambiswas6148
    @anupambiswas6148 2 роки тому

    Thank you sir

  • @utkarshsingh5390
    @utkarshsingh5390 3 роки тому

    Season 2 done bhaiya

  • @SunnyGupta00
    @SunnyGupta00 3 роки тому

    Reach++ 🔥

  • @yajatdhawan1865
    @yajatdhawan1865 2 роки тому +4

    bro its giving TLE

    • @bitrish34
      @bitrish34 2 роки тому +2

      use ordered_map instead of map it will pass all test case

    • @shubhangverma4478
      @shubhangverma4478 2 роки тому +2

      @@bitrish34 not working

  • @madhabkafle8072
    @madhabkafle8072 3 роки тому

    🔥🔥🔥

  • @helloUser12373
    @helloUser12373 2 роки тому

    your solution is not accepted on gfg. Test cases are failing.

  • @Sonu-tg6tg
    @Sonu-tg6tg 3 роки тому

    Please make videos using JAVA