Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LFUCache {
- constructor(capacity) {
- this.capacity = capacity;
- this.storage = {};
- this.freq = [];
- this.size = 0;
- }
- get(k) {
- this.freq.push(k)
- return this.storage[k] ? this.storage[k] : -1
- }
- put(k, v) {
- this.freq.push(k);
- if (Object.keys(this.storage).length < 2 ) {
- this.storage[k] = v;
- } else {
- let lfu = this.freq.shift();
- console.log('lfu', lfu)
- }
- }
- }
- // Your LFUCache object will be instantiated and called as such:
- cache = new LFUCache( 2 /* capacity */ );
- cache.put(1, 1);
- cache.put(2, 2);
- cache.get(1); // returns 1
- cache.put(3, 3); // evicts key 2
- cache.get(2); // returns -1 (not found)
- cache.get(3); // returns 3.
- cache.put(4, 4); // evicts key 1.
- cache.get(1); // returns -1 (not found)
- cache.get(3); // returns 3
- cache.get(4); // returns 4
- console.log(cache)
Add Comment
Please, Sign In to add comment