Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var LFUCache = function(capacity) {
- this.storage = {};
- this.capacity = capacity;
- this.keys = [];
- };
- LFUCache.prototype.get = function(key) {
- var index = this.keys.indexOf(key);
- if (index > 0) {
- this.keys.splice(index, 1);
- this.keys.unshift(key);
- }
- return this.storage[key] ? this.storage[key] : -1
- };
- LFUCache.prototype.put = function(key, value) {
- var cacheCurrentLength = Object.keys(this.storage).length;
- if (cacheCurrentLength < this.capacity) {
- this.keys.unshift(key);
- this.storage[key] = value;
- } else {
- var LFUKey = this.keys.pop();
- delete this.storage[LFUKey];
- this.keys.unshift(key);
- this.storage[key] = value;
- }
- };
Add Comment
Please, Sign In to add comment