MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / LFUCache

Function LFUCache

javascript/0460-lfu-cache.js:61–86  ·  view source on GitHub ↗
(capacity)

Source from the content-addressed store, hash-verified

59 * @param {number} capacity
60 */
61var LFUCache = function (capacity) {
62 this.capacity = capacity;
63 this.valueMap = new Map();
64 this.countMap = new Map();
65 this.lftCount = 1;
66 this.lists = [];
67 this.counter = (key) => {
68 let count = this.countMap.get(key) || 0;
69 if (this.lists[count]) {
70 this.lists[count].pop(key);
71 }
72 if (!this.lists[count + 1]) {
73 this.lists[count + 1] = new LinkedList();
74 }
75 this.lists[count + 1].pushRight(key);
76 this.countMap.set(key, count + 1);
77 // console.log(this.lftCount == count, count, this.lists[count].len())
78 if (
79 this.lftCount == count &&
80 this.lists[count] &&
81 this.lists[count].len() === 0
82 ) {
83 this.lftCount++;
84 }
85 };
86};
87
88/**
89 * @param {number} key

Callers

nothing calls this directly

Calls 5

pushRightMethod · 0.80
lenMethod · 0.80
getMethod · 0.45
popMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected