Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. var LFUCache = function(capacity) {
  2. this.storage = {};
  3. this.capacity = capacity;
  4. this.keys = [];
  5. };
  6.  
  7. LFUCache.prototype.get = function(key) {
  8. var index = this.keys.indexOf(key);
  9. if (index > 0) {
  10. this.keys.splice(index, 1);
  11. this.keys.unshift(key);
  12. }
  13. return this.storage[key] ? this.storage[key] : -1
  14. };
  15.  
  16. LFUCache.prototype.put = function(key, value) {
  17. var cacheCurrentLength = Object.keys(this.storage).length;
  18. if (cacheCurrentLength < this.capacity) {
  19. this.keys.unshift(key);
  20. this.storage[key] = value;
  21. } else {
  22. var LFUKey = this.keys.pop();
  23. delete this.storage[LFUKey];
  24. this.keys.unshift(key);
  25. this.storage[key] = value;
  26. }
  27. };
Add Comment
Please, Sign In to add comment