Hey everyone. Check out this in-depth solution for leetcode 146.
Пікірлер: 17
@maxwong46574 ай бұрын
Thank you so much. This solution is the most understandable!
@carefree_ladka2 жыл бұрын
Just amazing. I'm preparing for DSA and this solution is just easy to understand. Thank you :)
@rickyu19782 жыл бұрын
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)
@21doyourthing Жыл бұрын
fantastic
@ImpendingSole132 жыл бұрын
Thanks !!!
@aishwaryavhatkar7230 Жыл бұрын
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?
@jamespeters80093 жыл бұрын
thannks for the explanations , how did you console.log out the result? or how would you I can see that
@oscaragbasi67242 жыл бұрын
console.log it will show as stdout in leetcode
@apoorvasa10262 жыл бұрын
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
@rickyu19782 жыл бұрын
in your put function, aren't you missing a else before this.map.set ? am i seeing this.map.set run twice?
@raysdev Жыл бұрын
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.
@ruthylevi98042 жыл бұрын
hi! can you upload the code? thank you :)
@andygala8882 жыл бұрын
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.
@ruthylevi98042 жыл бұрын
@@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.
@ruthylevi98042 жыл бұрын
your solution just ran on leetcode actually! :)
@michaelchiang23332 жыл бұрын
@@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 Жыл бұрын
@Ruthy Levi @Andy Gala the code failed for me for time on leet code. Did you make any tweeks to the code above?