Skip to content

Commit c83dae6

Browse files
committedMar 1, 2025
Add solution #460
1 parent a9eb0ee commit c83dae6

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
457|[Circular Array Loop](./0457-circular-array-loop.js)|Medium|
369369
458|[Poor Pigs](./0458-poor-pigs.js)|Hard|
370370
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
371+
460|[LFU Cache](./0460-lfu-cache.js)|Hard|
371372
461|[Hamming Distance](./0461-hamming-distance.js)|Easy|
372373
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|
373374
472|[Concatenated Words](./0472-concatenated-words.js)|Hard|

‎solutions/0460-lfu-cache.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* 460. LFU Cache
3+
* https://leetcode.com/problems/lfu-cache/
4+
* Difficulty: Hard
5+
*
6+
* Design and implement a data structure for a Least Frequently Used (LFU) cache.
7+
*
8+
* Implement the LFUCache class:
9+
* - LFUCache(int capacity) Initializes the object with the capacity of the data structure.
10+
* - int get(int key) Gets the value of the key if the key exists in the cache. Otherwise,
11+
* returns -1.
12+
* - void put(int key, int value) Update the value of the key if present, or inserts the
13+
* key if not already present. When the cache reaches its capacity, it should invalidate
14+
* and remove the least frequently used key before inserting a new item. For this problem,
15+
* when there is a tie (i.e., two or more keys with the same frequency), the least recently
16+
* used key would be invalidated.
17+
*
18+
* To determine the least frequently used key, a use counter is maintained for each key in
19+
* the cache. The key with the smallest use counter is the least frequently used key.
20+
*
21+
* When a key is first inserted into the cache, its use counter is set to 1 (due to the
22+
* put operation). The use counter for a key in the cache is incremented either a get or
23+
* put operation is called on it.
24+
*
25+
* The functions get and put must each run in O(1) average time complexity.
26+
*/
27+
28+
/**
29+
* @param {number} capacity
30+
*/
31+
var LFUCache = function(capacity) {
32+
this.capacity = capacity;
33+
this.size = 0;
34+
this.minFreq = 0;
35+
this.values = new Map();
36+
this.freq = new Map();
37+
this.keys = new Map();
38+
};
39+
40+
/**
41+
* @param {number} key
42+
* @return {number}
43+
*/
44+
LFUCache.prototype.get = function(key) {
45+
if (!this.values.has(key)) return -1;
46+
47+
const freq = this.freq.get(key);
48+
this.freq.set(key, freq + 1);
49+
50+
this.keys.get(freq).delete(key);
51+
if (!this.keys.get(freq).size) {
52+
this.keys.delete(freq);
53+
if (this.minFreq === freq) this.minFreq++;
54+
}
55+
56+
if (!this.keys.has(freq + 1)) this.keys.set(freq + 1, new Set());
57+
this.keys.get(freq + 1).add(key);
58+
59+
return this.values.get(key);
60+
};
61+
62+
/**
63+
* @param {number} key
64+
* @param {number} value
65+
* @return {void}
66+
*/
67+
LFUCache.prototype.put = function(key, value) {
68+
if (this.capacity === 0) return;
69+
70+
if (this.values.has(key)) {
71+
this.values.set(key, value);
72+
this.get(key);
73+
return;
74+
}
75+
76+
if (this.size === this.capacity) {
77+
const keyToRemove = this.keys.get(this.minFreq).values().next().value;
78+
this.keys.get(this.minFreq).delete(keyToRemove);
79+
if (!this.keys.get(this.minFreq).size) {
80+
this.keys.delete(this.minFreq);
81+
}
82+
this.values.delete(keyToRemove);
83+
this.freq.delete(keyToRemove);
84+
this.size--;
85+
}
86+
87+
this.values.set(key, value);
88+
this.freq.set(key, 1);
89+
if (!this.keys.has(1)) this.keys.set(1, new Set());
90+
this.keys.get(1).add(key);
91+
this.minFreq = 1;
92+
this.size++;
93+
};

0 commit comments

Comments
 (0)
Please sign in to comment.