LRU Cache (Rust)
Source: LeetCode #146
Topics: data structures, caching
Problem
Implement a fixed-capacity Least-Recently-Used cache of i32 -> i32:
pub struct LruCache { /* your fields */ }
impl LruCache {
pub fn new(capacity: usize) -> Self;
pub fn get(&mut self, key: i32) -> Option<i32>; // marks key most-recently-used
pub fn put(&mut self, key: i32, value: i32); // insert/update; evict LRU when full
}
get returns the value and refreshes recency, or None.
put inserts or updates and refreshes recency; when over capacity, evict the
least-recently-used entry.
Grading
Your solution.rs is compiled together with a trusted tests.rs (which include!s it) using
rustc --test. Only solution.rs is yours to edit.
Sign in to submit your solution.