LEETCODE 146 (JAVASCRIPT) | LRU CACHE

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 18

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

    Just amazing. I'm preparing for DSA and this solution is just easy to understand. Thank you :)

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

    Instead of the for loop, you could get the first element by destructuring the map
    const [firstKey] = map,keys
    this gives you the key of the first index and you can then map.delete(firstKey)

  • @maxwong4657
    @maxwong4657 10 місяців тому

    Thank you so much. This solution is the most understandable!

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

    thannks for the explanations , how did you console.log out the result? or how would you I can see that

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

      console.log it will show as stdout in leetcode

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

      let lRUCache = new LRUCache(2)
      console.log(lRUCache.put(1, 1)); // cache is {1=1}
      console.log(lRUCache.put(2, 2)); // cache is {1=1, 2=2}
      console.log(lRUCache.get(1)); // return 1
      console.log(lRUCache.put(3, 3)); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
      console.log(lRUCache.get(2)); // returns -1 (not found)
      console.log(lRUCache.put(4, 4)); // LRU key was 1, evicts key 1, cache is {3=3, 4=4}
      console.log(lRUCache.get(1)); // return -1 (not found)
      console.log(lRUCache.get(3)); // return 3
      console.log(lRUCache.get(4)); // return 4

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

    Great thanks a lot. Was struggling to understand the LRU and MRU!
    Implemented the solution but its giving "Time Limit Exceeded" error. Doesnt the for loop run in O(N) time?

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

    in your put function, aren't you missing a else before this.map.set ? am i seeing this.map.set run twice?

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

      in the outer if block it will delete the matching key:val from the map when both the key doesn't exist & at capacity.
      in the case that the key does exist, then it will invoke the get() which in turn invokes map.set() but to the same value it was initially but relocated to the MRU position if it wasn't there already.
      the final map.set() in the put(key, val) is invoked again, but this time w/ the updated val for that key.
      so, yes for that case the set() method is invoked twice so not as efficient.

  • @sk45861
    @sk45861 3 місяці тому

    Small request, please use night mode, otherwise it's difficult to read

  • @21doyourthing
    @21doyourthing 2 роки тому

    fantastic

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

    hi! can you upload the code? thank you :)

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

      Hey Ruthy, this solution won’t run. It uses the map data structure which times out. If you need the tests to pass you have to use the doubly linked list approach. I’ll try to make a video at soon covering this.

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

      @@andygala888 oh gotcha - that's unfortunate because I liked this solution way better lol. thanks for letting me know though, and that video would be appreciated whenever you can! your channel is always a huge help.

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

      your solution just ran on leetcode actually! :)

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

      @@ruthylevi9804 This map solution ran for me too but I noticed that it's only 12.19% faster than all of the other Javascript submissions. The doubly linked list solution might be more optimized but I agree that I like this solution way better since we're only using a hash map. Just a heads up that in an interview setting, they might be expecting the doubly linked list + hash map solution.

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

      @Ruthy Levi @Andy Gala the code failed for me for time on leet code. Did you make any tweeks to the code above?

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

    Thanks !!!