forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFUCache.java
127 lines (104 loc) · 3.82 KB
/
LFUCache.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.leetcode.design;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Hard
* Link: https://leetcode.com/problems/lfu-cache/
* Description:
* Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following
* operations: get and put.
*
* get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
* put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it
* should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when
* there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.
*
* Follow up:
* Could you do both operations in O(1) time complexity?
*
* Note:
* -
*
* @author rampatra
* @since 2019-08-20
*/
public class LFUCache {
private int minCount = 0;
private int capacity;
private Map<Integer, Integer> keyValueMap;
private Map<Integer, Integer> keyCountMap;
private Map<Integer, LinkedHashSet<Integer>> countKeysMap;
public LFUCache(int capacity) {
this.capacity = capacity;
keyValueMap = new HashMap<>();
keyCountMap = new HashMap<>();
countKeysMap = new HashMap<>();
}
public int get(int key) {
Integer val = keyValueMap.get(key);
if (val == null) {
return -1;
}
int prevCount = keyCountMap.get(key);
countKeysMap.getOrDefault(prevCount, new LinkedHashSet<>()).remove(key);
countKeysMap.putIfAbsent(prevCount + 1, new LinkedHashSet<>());
countKeysMap.get(prevCount + 1).add(key);
if (prevCount == minCount && countKeysMap.get(prevCount).size() == 0) {
minCount++;
}
return val;
}
public void put(int key, int value) {
if (capacity <= 0) {
return;
}
if (!keyValueMap.containsKey(key) && keyCountMap.size() == capacity) {
int keyToEvict = countKeysMap.get(minCount).iterator().next();
countKeysMap.get(minCount).remove(keyToEvict);
keyValueMap.remove(keyToEvict);
keyCountMap.remove(keyToEvict);
}
keyValueMap.put(key, value);
int prevCount = keyCountMap.getOrDefault(key, 0);
keyCountMap.put(key, 1);
countKeysMap.getOrDefault(prevCount, new LinkedHashSet<>()).remove(key);
countKeysMap.putIfAbsent(1, new LinkedHashSet<>());
countKeysMap.get(1).add(key);
minCount = 1;
}
public static void main(String[] args) {
LFUCache lfuCache = new LFUCache(2);
lfuCache.put(2, 2);
lfuCache.put(3, 3);
lfuCache.put(4, 4);
assertEquals(-1, lfuCache.get(2));
assertEquals(3, lfuCache.get(3));
lfuCache.put(5, 5);
assertEquals(-1, lfuCache.get(4));
lfuCache = new LFUCache(2);
lfuCache.put(3, 1);
lfuCache.put(2, 1);
lfuCache.put(2, 2);
lfuCache.put(4, 4);
assertEquals(-1, lfuCache.get(3));
assertEquals(2, lfuCache.get(2));
lfuCache = new LFUCache(2);
lfuCache.put(2, 1);
lfuCache.put(2, 2);
assertEquals(2, lfuCache.get(2));
lfuCache.put(1, 1);
lfuCache.put(4, 4);
assertEquals(2, lfuCache.get(2));
lfuCache = new LFUCache(2);
assertEquals(-1, lfuCache.get(2));
lfuCache.put(2, 6);
assertEquals(-1, lfuCache.get(1));
lfuCache.put(1, 1);
lfuCache.put(1, 2);
assertEquals(2, lfuCache.get(1));
assertEquals(6, lfuCache.get(2));
// todo fix some test cases https://leetcode.com/submissions/detail/253376947/
}
}