Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var LFUCache = function(capacity) {
- this.storage = {};
- this.usage = {};
- this.size = 0;
- this.capacity = capacity;
- };
- LFUCache.prototype.get = function(key) {
- if (this.storage[key]) {
- this.usage[this.storage[key].uses]; // Before incrementing uses, we have to remove it from the array at the current usage value...
- this.storage[key].uses += 1;
- this.usage[this.storage[key].uses]; // ...then we have to push it onto the array for the new usage value.
- return this.storage[key].value;
- } else {
- return -1;
- }
- };
- LFUCache.prototype.put = function(key, value) {
- if (this.size < this.capacity) {
- if (this.storage[key]) {
- this.storage[key].value = value;
- // this.storage[key].uses += 1; // Problem doesn't specify whether reassignment counts as "use"; assuming NO.
- } else {
- this.storage[key] = { value: value, uses: 0 };
- }
- } else {
- }
- // else,
- // find least used value (key?)
- // we'll do this by checking possible usage values in the usage object, starting from 0
- // at the first usage array of length > 0, shift the key out of it and delete that key/value pair from storage
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement