| 59 | * @param {number} capacity |
| 60 | */ |
| 61 | var 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 |