Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class LFUCache {
  2. constructor(capacity) {
  3. this.capacity = capacity;
  4. this.storage = {};
  5. this.freq = [];
  6. this.size = 0;
  7. }
  8.  
  9. get(k) {
  10. this.freq.push(k)
  11. return this.storage[k] ? this.storage[k] : -1
  12. }
  13.  
  14. put(k, v) {
  15. this.freq.push(k);
  16. if (Object.keys(this.storage).length < 2 ) {
  17. this.storage[k] = v;
  18. } else {
  19. let lfu = this.freq.shift();
  20. console.log('lfu', lfu)
  21. }
  22.  
  23. }
  24. }
  25.  
  26. // Your LFUCache object will be instantiated and called as such:
  27. cache = new LFUCache( 2 /* capacity */ );
  28.  
  29. cache.put(1, 1);
  30. cache.put(2, 2);
  31. cache.get(1); // returns 1
  32. cache.put(3, 3); // evicts key 2
  33. cache.get(2); // returns -1 (not found)
  34. cache.get(3); // returns 3.
  35. cache.put(4, 4); // evicts key 1.
  36. cache.get(1); // returns -1 (not found)
  37. cache.get(3); // returns 3
  38. cache.get(4); // returns 4
  39. console.log(cache)
Add Comment
Please, Sign In to add comment