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)
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?
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.
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.
@@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 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.
Just amazing. I'm preparing for DSA and this solution is just easy to understand. Thank you :)
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)
Thank you so much. This solution is the most understandable!
thannks for the explanations , how did you console.log out the result? or how would you I can see that
console.log it will show as stdout in leetcode
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
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?
in your put function, aren't you missing a else before this.map.set ? am i seeing this.map.set run twice?
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.
Small request, please use night mode, otherwise it's difficult to read
fantastic
hi! can you upload the code? thank you :)
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.
@@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.
your solution just ran on leetcode actually! :)
@@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.
@Ruthy Levi @Andy Gala the code failed for me for time on leet code. Did you make any tweeks to the code above?
Thanks !!!