-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay80.java
More file actions
73 lines (67 loc) · 2.16 KB
/
Day80.java
File metadata and controls
73 lines (67 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
class LFUCache {
private final Map<Integer, Integer> vals; // Key-Value pairs
private final Map<Integer, Integer> counts; // Key-Use counts
private final Map<Integer, LinkedHashSet<Integer>> lists; // Use count-Keys mapping
private int minCount; // Minimum use count
private final int capacity; // Cache capacity
public LFUCache(int capacity) {
this.capacity = capacity;
this.minCount = 0;
this.vals = new HashMap<>();
this.counts = new HashMap<>();
this.lists = new HashMap<>();
}
public int get(int key) {
if (!vals.containsKey(key)) {
return -1;
}
int count = counts.get(key);
counts.put(key, count + 1);
lists.get(count).remove(key);
if (count == minCount && lists.get(count).size() == 0) {
minCount++;
}
if (!lists.containsKey(count + 1)) {
lists.put(count + 1, new LinkedHashSet<>());
}
lists.get(count + 1).add(key);
return vals.get(key);
}
public void put(int key, int value) {
if (capacity <= 0) {
return;
}
if (vals.containsKey(key)) {
vals.put(key, value);
get(key); // Increment count
return;
}
if (vals.size() >= capacity) {
int evict = lists.get(minCount).iterator().next();
lists.get(minCount).remove(evict);
vals.remove(evict);
counts.remove(evict);
}
vals.put(key, value);
counts.put(key, 1);
minCount = 1;
if (!lists.containsKey(1)) {
lists.put(1, new LinkedHashSet<>());
}
lists.get(1).add(key);
}
}
public class Day80 {
public static void main(String[] args) {
LFUCache lfuCache = new LFUCache(2);
lfuCache.put(1, 1);
lfuCache.put(2, 2);
System.out.print(lfuCache.get(1) + " "); // Output: 1
lfuCache.put(3, 3);
System.out.print(lfuCache.get(2) + " "); // Output: -1
System.out.println(lfuCache.get(3)); // Output: 3
}
}