LEETCODE 146 (JAVASCRIPT) | LRU CACHE

  Рет қаралды 4,387

Andy Gala

Andy Gala

3 жыл бұрын

Hey everyone. Check out this in-depth solution for leetcode 146.

Пікірлер: 17
@maxwong4657
@maxwong4657 4 ай бұрын
Thank you so much. This solution is the most understandable!
@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 жыл бұрын
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
@21doyourthing Жыл бұрын
fantastic
@ImpendingSole13
@ImpendingSole13 2 жыл бұрын
Thanks !!!
@aishwaryavhatkar7230
@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?
@jamespeters8009
@jamespeters8009 3 жыл бұрын
thannks for the explanations , how did you console.log out the result? or how would you I can see that
@oscaragbasi6724
@oscaragbasi6724 2 жыл бұрын
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
@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 Жыл бұрын
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.
@ruthylevi9804
@ruthylevi9804 2 жыл бұрын
hi! can you upload the code? thank you :)
@andygala888
@andygala888 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 жыл бұрын
@@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 жыл бұрын
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 Жыл бұрын
@Ruthy Levi @Andy Gala the code failed for me for time on leet code. Did you make any tweeks to the code above?
LEETCODE 155 (JAVASCRIPT) | MIN STACK
12:00
Andy Gala
Рет қаралды 3,7 М.
LEETCODE 238 (JAVASCRIPT) | PRODUCT OF ARRAY EXCEPT SELF
15:48
Andy Gala
Рет қаралды 4,1 М.
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 44 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 120 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 38 МЛН
LEETCODE 15 (JAVASCRIPT) | 3SUM
25:04
Andy Gala
Рет қаралды 6 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 116 М.
LRU Cache - Twitch Interview Question - Leetcode 146
17:49
NeetCode
Рет қаралды 253 М.
LEETCODE 56 (JAVASCRIPT) | MERGE INTERVALS
13:43
Andy Gala
Рет қаралды 4,3 М.
LEETCODE 78 (JAVASCRIPT) | SUBSETS I
13:51
Andy Gala
Рет қаралды 7 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 159 М.
How to Implement an LRU Cache (Leetcode #146 explained)
10:32
Byte by Byte
Рет қаралды 33 М.
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 44 МЛН