LRU: Least Recently Used
LFU: Least Frequently Used
146. LRU 缓存
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
- LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
- int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
- void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
示例:
输入 [“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] 输出 [null, null, null, 1, null, -1, null, -1, 3, 4] 解释 LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // 缓存是 {1=1} lRUCache.put(2, 2); // 缓存是 {1=1, 2=2} lRUCache.get(1); // 返回 1 lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3} lRUCache.get(2); // 返回 -1 (未找到) lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3} lRUCache.get(1); // 返回 -1 (未找到) lRUCache.get(3); // 返回 3 lRUCache.get(4); // 返回 4
提示:
- 1 <= capacity <= 3000
- 0 <= key <= 10000
- 0 <= value <= 105
- 最多调用 2 * 105 次 get 和 put
思路:
哈希表 + 双链表
从队头到队尾维护最近使用的节点(使用时间戳队头比队尾更接近现在时间)
class LRUCache {private class Node {int key, val;Node left, right;}Node hh, tt;Map<Integer, Node> map;int n;public LRUCache(int capacity) {this.n = capacity;map = new HashMap<>();hh = new Node();tt = new Node();hh.right = tt;tt.left = hh;}public int get(int key) {if (map.containsKey(key)) {Node node = map.get(key);delete(node);add(node);return node.val;} else return -1;}public void put(int key, int value) {if (map.containsKey(key)) {Node node = map.get(key);node.val = value;delete(node);add(node);} else {Node node = new Node();node.val = value;node.key = key;if (map.size() == n) {Node p = tt.left;delete(p);map.remove(p.key);}map.put(key, node);add(node);}}private void add(Node node) {node.right = hh.right;node.left = hh;hh.right.left = node;hh.right = node;}private void delete(Node node) {node.left.right = node.right;node.right.left = node.left;}}/*** Your LRUCache object will be instantiated and called as such:* LRUCache obj = new LRUCache(capacity);* int param_1 = obj.get(key);* obj.put(key,value);*/
460. LFU 缓存
请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。
实现 LFUCache 类:
- LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象
- int get(int key) - 如果键 key 存在于缓存中,则获取键的值,否则返回 -1 。
- void put(int key, int value) - 如果键 key 已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量 capacity 时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最近最久未使用 的键。
为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。
当一个键首次插入到缓存中时,它的使用计数器被设置为 1 (由于 put 操作)。对缓存中的键执行 get 或 put 操作,使用计数器的值将会递增。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
示例:
输入: [“LFUCache”, “put”, “put”, “get”, “put”, “get”, “get”, “put”, “get”, “get”, “get”] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] 输出: [null, null, null, 1, null, -1, 3, null, -1, 3, 4] 解释: // cnt(x) = 键 x 的使用计数 // cache=[] 将显示最后一次使用的顺序(最左边的元素是最近的) LFUCache lfu = new LFUCache(2); lfu.put(1, 1); // cache=[1,_], cnt(1)=1 lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 lfu.get(1); // 返回 1 // cache=[1,2], cnt(2)=1, cnt(1)=2 lfu.put(3, 3); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小 // cache=[3,1], cnt(3)=1, cnt(1)=2 lfu.get(2); // 返回 -1(未找到) lfu.get(3); // 返回 3 // cache=[3,1], cnt(3)=2, cnt(1)=2 lfu.put(4, 4); // 去除键 1 ,1 和 3 的 cnt 相同,但 1 最久未使用 // cache=[4,3], cnt(4)=1, cnt(3)=2 lfu.get(1); // 返回 -1(未找到) lfu.get(3); // 返回 3 // cache=[3,4], cnt(4)=1, cnt(3)=3 lfu.get(4); // 返回 4 // cache=[3,4], cnt(4)=2, cnt(3)=3
提示:
- 0 <= capacity <= 104
- 0 <= key <= 105
- 0 <= value <= 109
- 最多调用 2 * 105 次 get 和 put 方法
思路:
双哈希表 + 双链表套双链表
class LFUCache {class Node {int key, val;Node left, right;Node(){};Node(int key, int val) {this.key = key;this.val = val;}}class Block {int cnt;Node hh, tt;Block left, right;Block(int cnt) {this.cnt = cnt;hh = new Node();tt = new Node();hh.right = tt;tt.left = hh;}void delete(Node node) {node.right.left = node.left;node.left.right = node.right;}void add(Node node) {node.right = hh.right;node.left = hh;hh.right.left = node;hh.right = node;}boolean isEmpty() {return hh.right == tt;}}Block hh, tt;int n;Map<Integer, Node> map_node;Map<Integer, Block> map_block;public LFUCache(int capacity) {this.n = capacity;hh = new Block(0);tt = new Block(Integer.MAX_VALUE);hh.right = tt;tt.left = hh;map_block = new HashMap<>();map_node = new HashMap<>();}public int get(int key) {if (!map_node.containsKey(key)) return -1;Block block = map_block.get(key);Node node = map_node.get(key);block.delete(node);if (block.cnt + 1 != block.right.cnt)add(block, new Block(block.cnt + 1));Block ne = block.right;ne.add(node);map_block.put(key, ne);if (block.isEmpty()) {delete(block);}return node.val;}public void put(int key, int value) {if (n == 0) return;if (map_node.containsKey(key)) {Node node = map_node.get(key);node.val = value;get(key);} else {if (map_node.size() == n) {Node node = hh.right.tt.left;map_node.remove(node.key);map_block.remove(node.key);hh.right.delete(hh.right.tt.left);if (hh.right.isEmpty())delete(hh.right);}Node node = new Node(key, value);map_node.put(key, node);if (hh.right.cnt == 1) {Block block = hh.right;map_block.put(key, block);block.add(node);} else {Block block = new Block(1);add(hh, block);block.add(node);map_block.put(key, block);}}}void delete(Block block) {block.right.left = block.left;block.left.right = block.right;}void add(Block left, Block block) {block.right = left.right;block.left = left;left.right.left = block;left.right = block;}}/*** Your LFUCache object will be instantiated and called as such:* LFUCache obj = new LFUCache(capacity);* int param_1 = obj.get(key);* obj.put(key,value);*/
类似的问题:
| 432. 全 O(1) 的数据结构 | LFU的简易版 |
|---|---|
