Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the LFUCache class:
LFUCache(int capacity) Initializes the object with the capacity of the data structure.
int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
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.
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.
The functions get and put must each run in O(1) average time complexity.
Example 1:
Input
[“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]]
Output
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
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); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[4,3], cnt(4)=2, cnt(3)=3
Constraints:
- 0 <= capacity <= 104
- 0 <= key <= 105
- 0 <= value <= 109
- At most 2 * 105 calls will be made to get and put.
map 为 HashMap<key, (key, value, frequency)>
freqs 为 HashMap<frequency, DoubleLinkedList<(key, value, frequency)>>
get(key)时先检查 map[key]是否存在, 如果存在则:
- freqs[map[key].frequency].remove(map[key]),
- map[key].frequency += 1
- freqs[map[key].frequency].push(map[key])
put(key, value)时:
如果 map[key]存在, 则按照 get()的方法更新 map 和 freqs 中的 frequency, 同时更新 value
如果 map[key]不存在, 但是 map.len()还没到 capacity 时, map.insert(key, (key, value, 1)), freqs[1].push((key, value, 1))
如果 map.len()已经达到 capacity, 则进行 freqs[min_freq].left_pop(), 其中 min_freq 是在 cache 中维护的最小的 frequency, 该值只在更新 frequency 的时候进行更新, 假设我们 get(key), map[key].frequency 会增加 1, 如果 map[key].frequency == min_freq 且 freqs[frequency].remove(map[key])之后 freqs[frequency].len() == 0, 这时我们就需要更新 min_freq += 1。因为原 item 的 frequency + 1, 所以 min_freq += 1 后至少有一个 item, 不用担心 freqs[min_freq]为空的问题。
freqs[min_freq].left_pop()之后需要删掉 map 中相应的入口, 假设 item = freqs[min_freq].left_pop(), 我们需要 map.remove(item.key)。最后 map.insert(key, (key, value, 1)), freqs[1].push((key, value, 1))
这个题我觉得难的地方不在于解题的思路, 而是有些语言需要自己去实现 double linked list, 这方面 rust 确实不太擅长, 如果实现的不好很容易导致代码的冗长杂乱
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
type RcNode<T> = Rc<RefCell<Node<T>>>;
#[derive(Debug)]
struct Node<T> {
val: T,
prev: Option<RcNode<T>>,
next: Option<RcNode<T>>,
}
impl<T> Node<T> {
fn new(val: T) -> Self {
Self {
val,
prev: None,
next: None,
}
}
}
#[derive(Debug)]
struct List<T> {
head: Option<RcNode<T>>,
tail: Option<RcNode<T>>,
}
impl<T> List<T> {
fn new() -> Self {
Self {
head: None,
tail: None,
}
}
fn is_empty(&self) -> bool {
self.head.is_none() && self.tail.is_none()
}
fn push(&mut self, node: RcNode<T>) {
if self.is_empty() {
self.head = Some(node.clone());
self.tail = Some(node.clone());
return;
}
let tail = self.tail.take().unwrap();
tail.borrow_mut().next = Some(node.clone());
node.borrow_mut().prev = Some(tail.clone());
self.tail = Some(node.clone());
}
fn left_pop(&mut self) -> Option<RcNode<T>> {
if let Some(head) = self.head.take() {
if let Some(next) = head.borrow().next.as_ref() {
next.borrow_mut().prev = None;
self.head = Some(next.clone());
} else {
self.head = None;
self.tail = None;
}
return Some(head);
}
None
}
fn remove(&mut self, node: RcNode<T>) {
let mut b = node.borrow_mut();
if let Some(prev) = b.prev.take() {
if let Some(next) = b.next.take() {
prev.borrow_mut().next = Some(next.clone());
next.borrow_mut().prev = Some(prev.clone());
return;
}
prev.borrow_mut().next = None;
self.tail = Some(prev.clone());
return;
}
if let Some(next) = b.next.take() {
next.borrow_mut().prev = None;
self.head = Some(next.clone());
return;
}
self.head = None;
self.tail = None;
}
}
struct LFUCache {
cap: usize,
min_freq: i32,
cache: HashMap<i32, RcNode<(i32, i32, i32)>>,
freqs: HashMap<i32, List<(i32, i32, i32)>>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl LFUCache {
fn new(capacity: i32) -> Self {
Self {
cap: capacity as usize,
min_freq: 1,
cache: HashMap::new(),
freqs: HashMap::new(),
}
}
fn get(&mut self, key: i32) -> i32 {
if self.cap == 0 {
return -1;
}
if let Some(node) = self.cache.remove(&key) {
let (_, v, ori_freq) = node.borrow().val;
node.borrow_mut().val.2 += 1;
let mut list = self.freqs.remove(&ori_freq).unwrap();
list.remove(node.clone());
if list.is_empty() {
if ori_freq == self.min_freq {
self.min_freq += 1;
}
} else {
self.freqs.insert(ori_freq, list);
}
self.freqs
.entry(ori_freq + 1)
.or_insert(List::new())
.push(node.clone());
self.cache.insert(key, node.clone());
return v;
}
-1
}
fn put(&mut self, key: i32, value: i32) {
if self.cap == 0 {
return;
}
if let Some(node) = self.cache.remove(&key) {
let (_, _, ori_freq) = node.borrow().val;
node.borrow_mut().val.1 = value;
node.borrow_mut().val.2 += 1;
let mut list = self.freqs.remove(&ori_freq).unwrap();
list.remove(node.clone());
if list.is_empty() {
if ori_freq == self.min_freq {
self.min_freq += 1;
}
} else {
self.freqs.insert(ori_freq, list);
}
self.freqs
.entry(ori_freq + 1)
.or_insert(List::new())
.push(node.clone());
self.cache.insert(key, node.clone());
return;
}
if self.cache.len() == self.cap {
let mut list = self.freqs.remove(&self.min_freq).unwrap();
let node = list.left_pop().unwrap();
if !list.is_empty() {
self.freqs.insert(self.min_freq, list);
}
let k = node.borrow().val.0;
self.cache.remove(&k);
}
self.min_freq = 1;
let node = Rc::new(RefCell::new(Node::new((key, value, 1))));
self.cache.insert(key, node.clone());
self.freqs
.entry(1)
.or_insert(List::new())
.push(node.clone());
}
}