Skip to content

Commit d9ca800

Browse files
Merge pull request #584 from priyashuu/LFU
Add Java implementation for LFU (Least Frequently Used) Cache
2 parents 081ead6 + b785dc9 commit d9ca800

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Algorithm Name:LFU Cache .
3+
* Programming Language: Java
4+
* Category: Linked List .
5+
* Difficulty Level: Hard
6+
*
7+
* Author: Priya Rani
8+
*
9+
* Algorithm Description:Design and implement a data structure for a Least Frequently Used (LFU) cache.
10+
11+
Implement the LFUCache class:
12+
13+
LFUCache(int capacity) Initializes the object with the capacity of the data structure.
14+
- int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
15+
- void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. - For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated and so on
16+
17+
18+
19+
* Time Complexity:O(1) (average) HashMap and LinkedHashSet operations (insert/remove/lookup) are O(1)
20+
* Space complexity: O(capacity)Stores up to capacity nodes and frequency mappings
21+
*/
22+
23+
import java.util.*;
24+
25+
class LFUCache {
26+
private final int capacity;
27+
private int minFreq;
28+
private final Map<Integer, Node> nodeMap;
29+
private final Map<Integer, LinkedHashSet<Node>> freqMap;
30+
31+
private static class Node {
32+
int key, value, freq;
33+
Node(int key, int value) {
34+
this.key = key;
35+
this.value = value;
36+
this.freq = 1;
37+
}
38+
}
39+
40+
public LFUCache(int capacity) {
41+
this.capacity = capacity;
42+
this.minFreq = 0;
43+
this.nodeMap = new HashMap<>();
44+
this.freqMap = new HashMap<>();
45+
}
46+
47+
public int get(int key) {
48+
if (!nodeMap.containsKey(key)) return -1;
49+
50+
Node node = nodeMap.get(key);
51+
updateFreq(node);
52+
return node.value;
53+
}
54+
55+
public void put(int key, int value) {
56+
if (capacity == 0) return;
57+
58+
if (nodeMap.containsKey(key)) {
59+
Node node = nodeMap.get(key);
60+
node.value = value;
61+
updateFreq(node);
62+
return;
63+
}
64+
65+
if (nodeMap.size() >= capacity) {
66+
// Remove LFU node
67+
LinkedHashSet<Node> minFreqNodes = freqMap.get(minFreq);
68+
Node toRemove = minFreqNodes.iterator().next();
69+
minFreqNodes.remove(toRemove);
70+
nodeMap.remove(toRemove.key);
71+
if (minFreqNodes.isEmpty()) freqMap.remove(minFreq);
72+
}
73+
74+
Node newNode = new Node(key, value);
75+
nodeMap.put(key, newNode);
76+
freqMap.computeIfAbsent(1, k -> new LinkedHashSet<>()).add(newNode);
77+
minFreq = 1;
78+
}
79+
80+
private void updateFreq(Node node) {
81+
int oldFreq = node.freq;
82+
LinkedHashSet<Node> oldSet = freqMap.get(oldFreq);
83+
oldSet.remove(node);
84+
if (oldSet.isEmpty()) {
85+
freqMap.remove(oldFreq);
86+
if (minFreq == oldFreq) minFreq++;
87+
}
88+
89+
node.freq++;
90+
freqMap.computeIfAbsent(node.freq, k -> new LinkedHashSet<>()).add(node);
91+
}
92+
93+
public static void main(String[] args) {
94+
LFUCache lfu = new LFUCache(2);
95+
lfu.put(1, 1);
96+
lfu.put(2, 2);
97+
System.out.println(lfu.get(1));
98+
lfu.put(3, 3);
99+
System.out.println(lfu.get(2));
100+
System.out.println(lfu.get(3));
101+
lfu.put(4, 4);
102+
System.out.println(lfu.get(1));
103+
System.out.println(lfu.get(3));
104+
System.out.println(lfu.get(4));
105+
}
106+
}

0 commit comments

Comments
 (0)