Implement LRU cache

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • This video shows how to implement LRU cache in the most efficient way. This explanation involves step by step optimization explanation with proper examples. We can implement LRU cache in just O(1) time for each query. As usual the CODE LINK is present below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: gist.github.co...

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

  • @ajutamang642
    @ajutamang642 4 ปีที่แล้ว +75

    my CS degree < Tech Dose Content. Keep making sir. I love the content and easy to understand the concepts.

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

      Thanks

    • @ArbindYadav-oc3zg
      @ArbindYadav-oc3zg 4 ปีที่แล้ว +1

      Damn True 😂😂

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      😅

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

      @@techdose4u Yes. This is very good and great explanation within 10 mins. Pls keep adding such puzzles/algorithms etc, thanks

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

      India me teri CSE ke degree mere chaddi se bhi kam value rakhti hai

  • @pratikjain3323
    @pratikjain3323 3 ปีที่แล้ว +5

    2 mins into the video, I already know the DS and Algo to be used. Amazing video!

  • @marhawk6468
    @marhawk6468 3 ปีที่แล้ว +21

    Bravo once again, I never got a CS degree so I missed out on a lot. your videos are a blessing!

  • @idgaf76
    @idgaf76 3 ปีที่แล้ว +4

    actually u r pretty gr8 to be a teacher...absolutely amazing

  • @SatishKumar-jb9qm
    @SatishKumar-jb9qm 3 ปีที่แล้ว +6

    I like the approach presented to arrive at the final solution - thank you for making this video.

  • @piyushagarwal3262
    @piyushagarwal3262 3 ปีที่แล้ว +2

    I love your way of teaching. Made all my doubts clear.

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

    Please correct me if i am wrong,
    Considering n=cache size and q=query size
    Even in hash approach the case where there is a page hit to remove the hitted node and keep it at the rear position it takes O(n) ,so for shifting in this case it takes O(n) and searching O(1) so considering time complexity is calculated referring to worst time complexity ,it takes O(n*q) time complexity even using the hash approach 😅

  • @hariprasadcr5936
    @hariprasadcr5936 3 ปีที่แล้ว +2

    Awesomeee...... I've been searching for this everywhere, and the explanation was amazing..... hats off man

  • @GauravSingh-bo1ys
    @GauravSingh-bo1ys 4 ปีที่แล้ว +7

    This is gold. Thank you very much for this.

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

    Approach is good sir.
    Writing code in DLL will be lengthy & more difficulty in handling boundary cases.Instead we can use queue ,code will be very compact & easy to handle.
    Sir please also make videos on DP question.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Actually this is just for understanding. We will use Deque for this. Internally implementation will be optimized as explained :)

    • @najimali32
      @najimali32 4 ปีที่แล้ว

      @@techdose4u okay sir.

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

    Thank you so much for such a wonderful explanation

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

    Brilliant video. Loved the way you explained optimisation stepwise. Thanks. Also, no unnecessary comments. Strictly content.

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

      Thanks

  • @yasvidobariya1247
    @yasvidobariya1247 4 ปีที่แล้ว +1

    Compare to other channel techdose is underrated...It never happens that i watched your video and feel like wasted time..u are awesome.

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

    awesome explanation. Thank you, sir, felt like remembered OS class in college, of course, this is better than that

  • @hangoutwithabhi
    @hangoutwithabhi 3 ปีที่แล้ว +2

    Instead of HashMap we can use Set to store current elements keep updating that on any change with O(1). Searching would also be O(1).

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

      Get is linear operation on set

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

    Knowledge sharing was good in session. Thanks

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

    Most rearly found explanation

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

    Very helpful, thank you.

  • @aditya234567
    @aditya234567 4 ปีที่แล้ว +6

    You didn't mention about updating an existing value, during which we erase existing node even if its in middle of linked list and put it in the front. This erase takes O(n)

    • @ShashankKumar-hq2cm
      @ShashankKumar-hq2cm 3 ปีที่แล้ว +1

      Yaa it will still take O(N) time complexity in this approach to, because you need to reach that particular position and than remove it.And than further add it at end.

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

      insert at beginning i.e. at head -> o(1)

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

    Nice but I believe u missed the last part in which we have to shift the requested page to the rightmost if gets found in cache

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Yes I missed it. Might have mentioned in someone's comment 😅

  • @ss-xh3hf
    @ss-xh3hf 4 ปีที่แล้ว +2

    thanks for showing how to optimize step by step

  • @satish6155
    @satish6155 3 ปีที่แล้ว +1

    I might be wrong but searching a key in a HashMap has worst complexity as O(n) because multiple elements(with same hashcode) can be present in same bucket.

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 3 ปีที่แล้ว +1

    very clear and well defined explanation. Thank you..........

  • @Sunny-qq6un
    @Sunny-qq6un 3 ปีที่แล้ว

    thanks man , now I am able to solve this question by your helps.

  • @shreyasingh542
    @shreyasingh542 3 ปีที่แล้ว +1

    You must be working really hard

  • @AYUSHISHARMABIT
    @AYUSHISHARMABIT 4 ปีที่แล้ว +1

    One of your best videos.
    Thank you.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Welcome :)

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

      Yt username me roll no. Likhne par exam me zyada aate hai kya ? Har koi likh rha hai 🌚

  • @pradiplamsal1403
    @pradiplamsal1403 4 ปีที่แล้ว +1

    You explain topics pretty well. Thanks for this one

  • @meghendrasharma787
    @meghendrasharma787 4 ปีที่แล้ว +3

    Sir can we use circular queue to reduce shifting since the insertion is always done at rear and deletion will be always done from front. also the time taken for both the operation will be 1

  • @md_aaqil8027
    @md_aaqil8027 4 ปีที่แล้ว +1

    Excellent Explanation sir and keep growing your channel

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

    Tum bahut mast kaam karta h bhai

  • @theprotagonist3906
    @theprotagonist3906 3 ปีที่แล้ว +1

    Liked, subscribed, shared ❤

  • @shahedanam2734
    @shahedanam2734 4 ปีที่แล้ว +1

    Thanks,it's a very good explanation.

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

    amazing explanation !!

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

      Thanks

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

    Does the Python's in build lru_cache works in the same way as you have written in your code?

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

    In searching suppose an element is present in cache in the middle then we have to move it to front ....it will take O(n) time...how to optimise I it? I am getting TLE for it.

  • @rahulchaubey8988
    @rahulchaubey8988 4 ปีที่แล้ว +1

    Super explaination.👌👌

  • @pranushaaa
    @pranushaaa 3 ปีที่แล้ว +1

    Thanks sir. Could understand easily

  • @Rajesh-rg6fw
    @Rajesh-rg6fw 2 ปีที่แล้ว

    The way u explained... It's brilliant.
    But i have 2 doubts
    1. Hash table size is, random or equal to window size and how to handle collision?
    2. What r it's real world implications ?

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

      Its real world application is related to the memory management of different process that operate in machines.

  • @subhamswain2566
    @subhamswain2566 4 ปีที่แล้ว +1

    ♥️❤️❤️❤️❤️this is awsome sir

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

    When we request 4 at 10:26 what if 4 is presented at 2 position?? Pls reply

  • @maherukhzafar3093
    @maherukhzafar3093 3 ปีที่แล้ว +1

    Wonderful explanation!!

  • @NikhilSharma-xv6gx
    @NikhilSharma-xv6gx 4 ปีที่แล้ว +4

    Great explanation as always.
    A minor doubt, why are we using Doubly Linked list? If its to link yhe previous node, can't we just algorithm for removing node in O(1)?

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

      Yes you can but then you won't be able to get the new rear page. The page that will be least recent after removing the current least recent page.
      Hence you will need to update to rear to the page 1 before the least recent page that's why doubly linked list.

  • @high-oncode7576
    @high-oncode7576 4 ปีที่แล้ว +4

    How its order of 1 time at 8:06
    suppose the question given is 1 2 3 "2"
    1=2=3 are in cache then if you found 2 then we need to make linked list 1=3=2 is it O(1) now? we need to find where is 2 in the list and put it to the back at front
    here ("=" means doubly linked list)

    • @ntesla5
      @ntesla5 4 ปีที่แล้ว +1

      refer his next video in which he explained the code of this

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

      Your point seems acceptable.

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

    very very nice video...but what was the point to store the address of the nodes into the map as a key?... we never made any use of them..is there any use of value (i.e the address of the keys) in the most optimised algorithm ?... please let me know... thanks in advance

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

    thank you for making this

  • @SatyaPrakash-gj5vp
    @SatyaPrakash-gj5vp 4 ปีที่แล้ว +2

    Really Neat Explanation!
    There was an interview question asked what data structure do you use for caching? As per my knowledge i know LinkedHashMap or LinkedHashSet but interviewer asked why not ConcurrentHashMap? Please explain.

    • @ap331
      @ap331 4 ปีที่แล้ว

      Then your answer would be...that i am not proficient in java...its a java concept

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

      Something related to thread safety?

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

      Btw why can't we use Singly Linked List?

  • @the_sweet_heaven
    @the_sweet_heaven 4 ปีที่แล้ว +1

    Very good content.

  • @LalitSharma1607
    @LalitSharma1607 4 ปีที่แล้ว +1

    Brilliant explanation, Thanks

  • @IT__KumariTejaswini-xv9ii
    @IT__KumariTejaswini-xv9ii 3 ปีที่แล้ว

    Instead of using doubly linked list, can we use queue? because it will also give the time complexity O(1) .. Removing from front nd adding on rear...

  • @SubhamKumar-9009
    @SubhamKumar-9009 3 ปีที่แล้ว

    nice

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

    i had implemented the code using singly linked list. the code is given below if there is any mistake please correct it
    #include
    using namespace std;
    struct node
    {
    int data;
    struct node *next;
    };
    int main()
    {
    int arr[] = {1, 2, 3, 4, 1, 3};
    int n = sizeof(arr) / sizeof(int);
    node *front;
    node *rear;
    node *temp;
    node *temp1;
    front = (struct node *)malloc(sizeof(struct node));
    rear = (struct node *)malloc(sizeof(struct node));
    temp = (struct node *)malloc(sizeof(struct node));
    temp1 = (struct node *)malloc(sizeof(struct node));
    front->data = -1;
    rear->data = -1;
    int k = 3;
    for (int i = 0; i < k; i++)
    {
    cout data == -1)
    {
    cout data = arr[i];
    front->data = arr[i];
    rear = front;
    }
    else
    {
    cout next = (struct node *)malloc(sizeof(struct node));
    front->next->data = arr[i];
    front = front->next;
    front->next = NULL;
    }
    }
    cout 0)
    {
    cout data)
    {
    cout next;
    front->data = op;
    }
    else
    {
    int data = temp->data;
    temp->next = temp->next->next;
    front->next = (struct node *)malloc(sizeof(struct node));
    front = front->next;
    front->data = data;
    }
    }
    else
    {
    cout data;
    rear = rear->next;
    front->next = (struct node *)malloc(sizeof(struct node));
    cout

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

    Thanks a lot man !This was an excellent explaination.Just one doubt when we remove an element from an unorderedmap will it be a O(n) operation or O(1) operation??

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

      O(n) worst case. Please read docs.

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

    very good insights

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

    can anyone tell me which tool is he using for writing the explanations

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

    Thank you so much TECH DOSE. :3

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

    Can anyone help me to understand how search operation in Deque takes O(1)? Searching any random index doesn't take O(n) complexity?

  • @user-bu8mg7uq3s
    @user-bu8mg7uq3s ปีที่แล้ว +1

    thank you

  • @mujahidpasha4440
    @mujahidpasha4440 4 ปีที่แล้ว +1

    Can you explain which is the best algorithm to find the shortest path in a graph... I know there are a bunch of algorithms for that among that which is the optimal and why... Thanks again

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Just stick with Dijkstra or else bellman ford for negative edges. It will work.

  • @adityapandey7516
    @adityapandey7516 4 ปีที่แล้ว +1

    Sir can't we use Deque here??

  • @jagadeeshr91
    @jagadeeshr91 3 ปีที่แล้ว +1

    best one

  • @vaibhavchawla2439
    @vaibhavchawla2439 4 ปีที่แล้ว +1

    Space complexity is O(size of cache) ? Asking because it is not mentioned in the video.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      If you are just using cache then you are correct :)

    • @vaibhavchawla2439
      @vaibhavchawla2439 4 ปีที่แล้ว

      @@techdose4u What are the other things am i missing something?

  • @dheerajpoonia2175
    @dheerajpoonia2175 4 ปีที่แล้ว +3

    Sir please make video on FARTHEST SEARCH FIRST cache algorithm, it's is difficult to under please

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

      Problem added in todo list. Will make it once I process the earlier requested problems.

    • @adarshsharmanit356
      @adarshsharmanit356 4 ปีที่แล้ว

      @@techdose4u Queue will be better in LRU cache

  • @parekshamanchanda8083
    @parekshamanchanda8083 4 ปีที่แล้ว +3

    This is how you do it in an interview ❤️❤️

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

    Thank for you

  • @mujahidpasha4440
    @mujahidpasha4440 4 ปีที่แล้ว +1

    You are awesome....

  • @radhu8
    @radhu8 3 ปีที่แล้ว +1

    Beautiful!

  • @HimanshuSharma-sd5gk
    @HimanshuSharma-sd5gk 3 ปีที่แล้ว

    Can we use a circular queue in place of linked list

  • @xlr8tanmay
    @xlr8tanmay 3 ปีที่แล้ว +1

    Why are we using a Doubly Linked List here?

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

      Easy to remove an element

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

    Hi Sir, why we didn`t use single LL like this... 1-> 2->3 if 4 comes then pointing the head to the 2 and at the tail 4 is added. In this way we don`t need to traverse back to update the variable name. So was there need of doubly. Please help !!

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

      If the user wants access the 4th which is at end of the LL we have to move that to the top
      In doubly and queue we can do that easily

  • @080somyasrivastava6
    @080somyasrivastava6 4 ปีที่แล้ว +1

    How to think which data structure is to be applied ?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Actually you need to figure out the goal of question and the operations you want to perform. Now from your knowledge, you will think what DS to apply to get the job done efficiently. The more you practice, the better you get at deciding good DS & Algo :)

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

    You need to update hashmap, it takes O(n) not O(1)

  • @gargsaumya20
    @gargsaumya20 4 ปีที่แล้ว

    I have a silly doubt. If we use hash function instead of shifting elements in array, then also we have a O(N) space complexity, then how can we say that it can be optimized..?Please bhaiya solve my doubt..

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

      array is fixed size ... won't grow dynamically so linked list is useful

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

    What if element is already present at the middle of linked list then removing it will take O(N) again right? Or can we go directly in O(1) since we have address of that element in hashmap?

    • @RAHULTHAKUR-ij5xu
      @RAHULTHAKUR-ij5xu 3 ปีที่แล้ว

      Yup ture hashmap stores address of linked lost nide, being doubly linked link it's easy to associate previous node to next node

  • @yitingg7942
    @yitingg7942 3 ปีที่แล้ว +1

    The deque.remove(element), is this O(1) time ?

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

      Gm 🌄 Don't think so.

    • @yitingg7942
      @yitingg7942 3 ปีที่แล้ว +1

      @@techdose4u So we can't use deque then ?

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

      @@yitingg7942 I think in LRU implementation, it could have been done using map if I remeber correctly, along with deque. Then it will be O(1) for remove. I will have to check it.

    • @yitingg7942
      @yitingg7942 3 ปีที่แล้ว +1

      @@techdose4u Thanks Surya, I am a little confused that yes by using hashMap we can search for O(1) to see if it's there, to remove in DLL it's O(1) as well. Yes I admit that. However doesn't it take Linear time to search the whole DLL to see position this element is in and then remove it ? I don't use anybody use deque in the discussion, only DLL. So I am very confused.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      @@yitingg7942 The time complexity is linear in the number of elements removed. It's implemented like a linked List in C++. It should be similar in Java as well. So yea, O(1) it is.

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

    Please explain in simple terms..the main purpose of us students is to solve problem.. not to figure out complex terminologies

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

    Why not single linked list????

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

    Can't weuse deque instead of a doubly linked list for O(1) deletion and insertion?

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

      yeah i also thought the same

    • @avinash-xh6qw
      @avinash-xh6qw 3 ปีที่แล้ว

      You can use Either Deque or List or DLL.

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

    you are switching rear with the front!

  • @rahulchaubey8988
    @rahulchaubey8988 4 ปีที่แล้ว +1

    👌👌👌👌

  • @somaanilsreeram9555
    @somaanilsreeram9555 3 ปีที่แล้ว +1

    Can you do a video on LFU cache as well?

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

      I will try that as well.

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

    Where is the code man

  • @ArpitDhamija
    @ArpitDhamija 4 ปีที่แล้ว +3

    IB me submit krte hue, 1000 points ke 200 ho gye 😂😂😢😢

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

      🤣 koi baat nhi....sikhne se mtlb hai :)

    • @ArpitDhamija
      @ArpitDhamija 4 ปีที่แล้ว

      @@techdose4u testcases of IB are horrible

    • @ArpitDhamija
      @ArpitDhamija 4 ปีที่แล้ว

      @@techdose4u sir I have few doubts in IB questions, can you make explanation video of those questions....

  • @aabhassaxena2490
    @aabhassaxena2490 4 ปีที่แล้ว +1

    1 2 3 4 5 6 3 5 6
    You did not covered handling of such cases!

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      I already covered. When 2nd time 5 comes then you need to reorder the DLL. I already told DLL reordering is required and done in O(1).

    • @aabhassaxena2490
      @aabhassaxena2490 4 ปีที่แล้ว

      But the deletion of node and changing of pointers u didn't showed,although u explained that

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

      For that we are already using DS and it is too trivial to explain.

  • @shaswatdas6553
    @shaswatdas6553 4 ปีที่แล้ว

    #Python code with OrderedDict only
    from collections import OrderedDict
    class LRU:
    def __init__(self, capacity: int):
    self.cache = OrderedDict()
    self.capacity = capacity
    def refer(self, k):
    print('-----------------Each Iter--------------------')
    print('k =', k)
    print(self.cache.keys())
    if k in self.cache:
    self.cache.move_to_end(k, last=False)
    return True
    else:
    self.cache[k] = True
    self.cache.move_to_end(k, last=False)
    if len(self.cache) > self.capacity:
    self.cache.popitem()
    return False
    # RUNNER
    # initializing our cache with the capacity of 2
    lru = LRU(4)
    print(lru.refer(1))
    print(lru.refer(2))
    print(lru.refer(3))
    print(lru.refer(1))
    print(lru.refer(4))
    print(lru.refer(5))
    print('------------------------Final------------------------')
    print(lru.cache.keys())

  • @shaswatdas6553
    @shaswatdas6553 4 ปีที่แล้ว

    #Python code with self implemented doubly linked list
    import gc
    class Node:
    def __init__(self, key):
    self.key = key
    self.next = self.prev = None
    def __str__(self):
    return f'{self.key}'
    class DoublyLL:
    def __init__(self):
    self.head = self.last = None
    self.size = 0
    def insertfirst(self, node):
    if self.head is None:
    self.head = self.last = node
    else:
    self.head.prev = node
    node.next = self.head
    self.head = node
    self.size += 1
    def deletenode(self, node=None):
    if node == None:
    node = self.last
    self.last = self.last.prev
    elif node.next == None:
    self.last = self.last.prev
    k = node.key
    if node.prev is not None:
    node.prev.next = node.next
    if node.next is not None:
    node.next.prev = node.prev
    node.next = node.prev = None
    gc.collect()
    self.size -= 1
    # print('deleted', k)
    return k
    class LRU:
    def __init__(self, n):
    self.dll = DoublyLL()
    self.dict = {}
    self.csize = n
    def refer(self, k):
    print('-----------------Each Iter--------------------')
    print('k =', k)
    print('Cache:\t', end='')
    self.print_cache()
    addr = self.dict.get(k)
    if addr != None and addr == self.dll.head:
    return True
    if self.dll.size == self.csize or addr != None:
    del_k = self.dll.deletenode(addr)
    del self.dict[del_k]
    node = Node(k)
    self.dll.insertfirst(node)
    self.dict[k] = node
    return True if addr is not None else False
    def print_cache(self):
    temp = self.dll.head
    while temp is not None:
    print(temp, end=' ')
    temp = temp.next
    print()
    lru = LRU(4)
    print(lru.refer(1))
    print(lru.refer(2))
    print(lru.refer(3))
    print(lru.refer(1))
    print(lru.refer(4))
    print(lru.refer(5))
    print('------------------------Final------------------------')
    lru.print_cache()
    print(lru.dll.head)

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

    Your implementation will fail for Test Case :
    1 2 3 1 4

  • @HomerCurry-m6i
    @HomerCurry-m6i 4 วันที่ผ่านมา

    Miller Mark Martin Richard Walker Michael

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

    Super explaination 👌👌