Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /**
  2. * @param {number} capacity
  3. */
  4. var LFUCache = function(capacity) {
  5.  
  6. // capacity is relative to size of LFUCache
  7. this.capacity = capacity;
  8. this.storage = {};
  9. // create counter variable for each insertion
  10. // each time there is same insertion, increase counter
  11. // when capacity is full, search for block with lowest counter and remove
  12.  
  13. };
  14.  
  15. /**
  16. * @param {number} key
  17. * @return {number}
  18. */
  19. LFUCache.prototype.get = function(key) {
  20. for( let i = 0; i < this.capacity; i++ ) {
  21. if( key === this.storage[i][0] ) {
  22. return this.storage[0][1]
  23. } else {
  24. return -1
  25. }
  26. }
  27. };
  28.  
  29. /**
  30. * @param {number} key
  31. * @param {number} value
  32. * @return {void}
  33. */
  34. LFUCache.prototype.put = function(key, value) {
  35. var counter = counter || 0;
  36. // check capacity is not full
  37. // if capacity is full,
  38. // look for lowest counter and delete
  39. this.storage[counter++] = [key, value]
  40. };
  41.  
  42. /**
  43. * Your LFUCache object will be instantiated and called as such:
  44. * var obj = Object.create(LFUCache).createNew(capacity)
  45. * var param_1 = obj.get(key)
  46. * obj.put(key,value)
  47. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement