LFU CACHE (Leetcode) - Code & Whiteboard

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

КОМЕНТАРІ • 34

  • @htchannel123
    @htchannel123 5 місяців тому

    Thank you so much for giving a great explanation.

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

    Hey, awesome video! This is super clear.
    In the case of needing to remove a key from the count dict that has multiple keys at the lowest count:
    I think we would want a doubly linked list so that we can remove the least recently used item at the head of the list.
    If we use a stack, popping the stack will get the most recent one put in that count index because it will be the last one in (LIFO).

    • @babybear-hq9yd
      @babybear-hq9yd  3 роки тому +2

      Hey Tryler, short answer: you're right. Longer answer: that's fundamentally how this solution is actually implemented, although it leverages some built in functionality within Python. In particular, you'll notice on line 10 that we're initializing an `OrderedDict`. This is the equivalent of a dictionary, but is actually backed by a linked list. When I'm calling the `popitem` function on line 35, I'm choosing to pop from the beginning and not the end (hence the `last=False` bit). With the OrderedDict, we can move any element within it to the front or back of the linked list, which allows us to pop items off in constant time. If you're either not familiar with Python, or not familiar with the underlying implementation of the `OrderedDict`, I agree that this definitely doesn't seem obvious.
      Did this address what you were mentioning? Or did I misinterpret the question? LMK!

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

      @@babybear-hq9yd Everything is good. Thank you so much for this video.

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

    Thank you for a clear explanantion.

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

    This was very tough for sure haha. I did it thanks to you! :)

  • @Cloud-577
    @Cloud-577 3 роки тому

    thank you that was really helpful and you explained it really well too

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

    Intuitively I solved this by using one dict and one DLL, where the DLL was ordered by frequency first and recency second, this way every time max capacity was reached I only had to remove the tail element. It got accepted but is only faster than 7%, is that fine or should I look at other approaches????

    • @babybear-hq9yd
      @babybear-hq9yd  2 роки тому

      On the surface it seems sound to me. Remember that the most important piece about your interviews will be the conversations you have in getting to the answer! Being open to discussing pros & cons of your proposal(s) and keeping an open dialogue on how to continuously improve the code is what will get you over the line, *not* necessarily having the 99.99th percentile fastest code.

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

    Hey please solve "The skyline problem" intuitively, it's hard to think about it. Thanks👍🏻

    • @babybear-hq9yd
      @babybear-hq9yd  3 роки тому +1

      Hey Sumit, I'll have a look at that one. I think I've gone through it before but didn't make a video about it. Hang tight!

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

      @@babybear-hq9yd Thanks👍🏻

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

    What hardware sketchpad are you using? Thanks for the explanation!

    • @babybear-hq9yd
      @babybear-hq9yd  3 роки тому +1

      it's the HUION 420 drawing pad. cheapest thing i could find on Amazon hahah

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

    can you please do LRU Cache?

    • @babybear-hq9yd
      @babybear-hq9yd  2 роки тому

      quite confident that one's outside my realm of expertise unfortunately

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

    Just so you know, your implementation leaves empty defaultdicts behind. It still works, but causes the memory usage to grow over time, despite the cache capacity.

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

      Nevermind, you address it later. Except it looks like you didn't note that you'll have to do it in put() as well.

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

    thanks for the great explanation! Is there any way you could also do Minimum Difficulty of a Job Schedule(LC 1335)? haha

    • @babybear-hq9yd
      @babybear-hq9yd  3 роки тому +1

      welcome! :) I've had that one requested a fair bit recently, but I don't have a tidy/working solution to it just yet. I'll see if I can get it done!

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

      @@babybear-hq9yd thanks for the reply! I will look forward to it

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

    I saw on leetcode there is a clever way to use a stack of stacks to solve this problem. Can you make a video explaining doing it that way?

    • @babybear-hq9yd
      @babybear-hq9yd  3 роки тому

      I remember trying to go through that as well but it didn't make much sense to me at the time -- not familiar enough with it bud, sorry!

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

    Great explanation, thank you

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

    West Virginia, mountain momma
    Take me home, country roads..!😇

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

    You don’t need an ordereddict, as regular dicts are already ordered as of Python 3.6 aren’t they? Good video though