Skip to content

Commit 62abcaf

Browse files
committed
LFU Cache: AC
1 parent 318737d commit 62abcaf

2 files changed

Lines changed: 45 additions & 16 deletions

File tree

LeetCodePrj/Java/leetcode/hard/page2/HardPage2.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
public class HardPage2 {
66
@Test
77
public void testLFUCache() {
8-
LFUCache lfuCache = new LFUCache(2);
9-
lfuCache.set(1,1);
10-
lfuCache.set(2,2);
8+
LFUCache lfuCache = new LFUCache(3);
9+
lfuCache.put(2,2);
10+
lfuCache.put(1,1);
11+
lfuCache.get(2);
1112
lfuCache.get(1);
12-
lfuCache.set(3,3);
1313
lfuCache.get(2);
14+
lfuCache.put(3,3);
15+
lfuCache.put(4,4);
1416
lfuCache.get(3);
15-
lfuCache.set(4,4);
17+
lfuCache.get(2);
1618
lfuCache.get(1);
17-
lfuCache.get(3);
1819
lfuCache.get(4);
1920
}
2021
}

LeetCodePrj/Java/leetcode/hard/page2/LFUCache.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,69 @@ public class LFUCache {
88
// <Key, Counts>
99
HashMap<Integer, Integer> counts;
1010
// <Counts, Key LinkedList>
11-
HashMap<Integer, LinkedHashSet<Integer>> lists;
11+
HashMap<Integer, LinkedHashSet<Integer>> order;
1212
int capacity;
1313
int min = 1;
1414

1515
public LFUCache(int capacity) {
1616
this.capacity = capacity;
1717
vals = new HashMap<>();
1818
counts = new HashMap<>();
19-
lists = new HashMap<>();
20-
lists.put(1, new LinkedHashSet<>());
19+
order = new HashMap<>();
20+
order.put(1, new LinkedHashSet<>());
2121
}
2222

2323
public int get(int key) {
2424
if (!vals.containsKey(key)) return -1;
2525

2626
int oldCount = counts.get(key);
2727
counts.put(key, oldCount + 1);
28-
if (oldCount == min && lists.get(oldCount).size() == 1) {
28+
order.get(oldCount).remove(key);
29+
if (oldCount == min && order.get(oldCount).size() == 0) {
2930
min = oldCount + 1;
3031
}
31-
lists.get(oldCount + 1).add(key);
32+
if (!order.containsKey(oldCount + 1)) {
33+
order.put(oldCount + 1, new LinkedHashSet<>());
34+
}
35+
order.get(oldCount + 1).add(key);
3236

3337
return vals.get(key);
3438
}
3539

36-
public void set(int key, int value) {
40+
public void put(int key, int value) {
41+
if (capacity == 0) return;
3742
if (vals.containsKey(key) || vals.size() < capacity) {
3843
vals.put(key, value);
39-
counts.put(key, counts.get(key) + 1);
40-
lists.get(counts.get(key)).add(key);
44+
int oldCount = counts.getOrDefault(key, 0);
45+
counts.put(key, oldCount + 1);
46+
47+
if (!order.containsKey(oldCount + 1)) {
48+
order.put(oldCount + 1, new LinkedHashSet<>());
49+
}
50+
order.get(oldCount + 1).add(key);
51+
if (oldCount == 0) {
52+
min = 1;
53+
} else {
54+
order.get(oldCount).remove(key);
55+
if (oldCount == min && order.get(oldCount).size() == 0) {
56+
min = oldCount + 1;
57+
}
58+
}
4159
} else {
42-
int removeKey = lists.get(min).iterator().next();
60+
// evict
61+
int removeKey = order.get(min).iterator().next();
4362
vals.remove(removeKey);
4463
counts.remove(removeKey);
45-
lists.get(min).remove(lists.get(min).iterator().next());
64+
order.get(min).remove(removeKey);
65+
66+
67+
min = 1;
68+
vals.put(key, value);
69+
counts.put(key, min);
70+
if (!order.containsKey(min)) {
71+
order.put(min, new LinkedHashSet<>());
72+
}
73+
order.get(min).add(key);
4674
}
4775
}
4876
}

0 commit comments

Comments
 (0)