Loading RustViz Engine...
Design and implement a Least Recently Used (LRU) Cache supporting get and put in O(1).
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
use std::collections::HashMap;
pub struct LRUCache {
capacity: usize,
map: HashMap<i32, i32>,
}
impl LRUCache {
pub fn new(capacity: usize) -> Self {
Self { capacity, map: HashMap::with_capacity(capacity) }
}
pub fn get(&mut self, key: i32) -> i32 {
*self.map.get(&key).unwrap_or(&-1)
}
pub fn put(&mut self, key: i32, value: i32) {
self.map.insert(key, value);
}
}