Created
September 14, 2017 16:52
-
-
Save tvolk131/dcb10b67eed2c74bddc7e3e4a6adf389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} capacity | |
*/ | |
var LFUCache = function(capacity) { | |
this.nodes = {}; | |
this.head = null; | |
this.tail = null; | |
this.size = 0; | |
this.capacity = capacity; | |
}; | |
/** | |
* @param {number} key | |
* @return {number} | |
*/ | |
LFUCache.prototype.get = function(key) { | |
let node = this.nodes[key]; | |
if (node) { | |
if (node.prev) { | |
node.prev.next = node.next; | |
} | |
if (node.next) { | |
node.next.prev = node.prev; | |
} | |
this.tail.next = node; | |
node.prev = this.tail; | |
node.next = null; | |
this.tail = this.tail.next; | |
} | |
return node ? node.value : -1; | |
}; | |
/** | |
* @param {number} key | |
* @param {number} value | |
* @return {void} | |
*/ | |
LFUCache.prototype.put = function(key, value) { | |
if (!this.nodes[value]) { | |
this.size++; | |
if (this.size === this.capacity) { | |
console.log(this.head); | |
this.head = this.head.next; | |
this.head.prev = null; | |
delete this.nodes[this.head.value]; | |
} | |
} | |
let node = new Node(value); | |
if (!this.head) { | |
this.head = this.tail = node; | |
} else { | |
this.tail.next = node; | |
node.prev = this.tail; | |
this.tail = this.tail.next; | |
} | |
this.nodes[value] = node; | |
}; | |
class Node { | |
constructor (value, next, prev) { | |
this.value = value; | |
this.next = next; | |
this.prev = prev; | |
} | |
} | |
/** | |
* Your LFUCache object will be instantiated and called as such: | |
* var obj = Object.create(LFUCache).createNew(capacity) | |
* var param_1 = obj.get(key) | |
* obj.put(key,value) | |
*/ | |
class TestSuite { | |
constructor () { | |
this.testsPassing = 0; | |
this.testsFailing = 0; | |
} | |
expect (valOne, valTwo, testName) { | |
if (JSON.stringify(valOne) === JSON.stringify(valTwo)) { | |
this.testsPassing++; | |
} else { | |
this.testsFailing++; | |
console.error(testName + ' - expected ' + valOne + ' to equal ' + valTwo); | |
} | |
} | |
printReport () { | |
console.log((this.testsPassing || 'No') + ' tests passing'); | |
console.log((this.testsFailing || 'No') + ' tests failing'); | |
} | |
run () { | |
let cache = new LFUCache(2); | |
cache.put(1, 1); | |
cache.put(2, 2); | |
this.expect(cache.get(1), 1, 'test'); | |
cache.put(3, 3); | |
this.expect(cache.get(2), -1, 'test'); | |
this.expect(cache.get(3), 3, 'test'); | |
cache.put(4, 4); | |
this.expect(cache.get(1), -1, 'test'); | |
this.expect(cache.get(3), 3, 'test'); | |
this.expect(cache.get(4), 4, 'test'); | |
this.printReport(); | |
} | |
} | |
new TestSuite().run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment