Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. var LFUCache = function(capacity) {
  2. this.storage = {};
  3. this.usage = {};
  4. this.size = 0;
  5. this.capacity = capacity;
  6. };
  7.  
  8. LFUCache.prototype.get = function(key) {
  9. if (this.storage[key]) {
  10. this.usage[this.storage[key].uses]; // Before incrementing uses, we have to remove it from the array at the current usage value...
  11. this.storage[key].uses += 1;
  12. this.usage[this.storage[key].uses]; // ...then we have to push it onto the array for the new usage value.
  13. return this.storage[key].value;
  14. } else {
  15. return -1;
  16. }
  17. };
  18.  
  19. LFUCache.prototype.put = function(key, value) {
  20. if (this.size < this.capacity) {
  21. if (this.storage[key]) {
  22. this.storage[key].value = value;
  23. // this.storage[key].uses += 1; // Problem doesn't specify whether reassignment counts as "use"; assuming NO.
  24. } else {
  25. this.storage[key] = { value: value, uses: 0 };
  26. }
  27. } else {
  28.  
  29. }
  30. // else,
  31. // find least used value (key?)
  32. // we'll do this by checking possible usage values in the usage object, starting from 0
  33. // at the first usage array of length > 0, shift the key out of it and delete that key/value pair from storage
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement