LEETCODE 146 (JAVASCRIPT) | LRU CACHE

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

Комментарии •

  • @carefree_ladka
    @carefree_ladka 2 года назад

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

  • @maxwong4657
    @maxwong4657 10 месяцев назад

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

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

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

  • @21doyourthing
    @21doyourthing 2 года назад

    fantastic

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

  • @sk45861
    @sk45861 4 месяца назад

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

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

  • @ImpendingSole13
    @ImpendingSole13 2 года назад

    Thanks !!!

  • @ruthylevi9804
    @ruthylevi9804 3 года назад +1

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

    • @andygala888
      @andygala888  3 года назад +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 3 года назад +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 3 года назад +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?