Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 97b175a

Browse files
committedSep 30, 2023
docs: add some documentation in lru cache
1 parent b15a9f3 commit 97b175a

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed
 

‎src/main.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
mod medium;
22

3-
use medium::reorder_list::ListNode;
4-
53
fn main() {
6-
// Reorder List - Medium
7-
let mut head = Some(Box::new(
8-
ListNode::new(1).add_next(
9-
ListNode::new(2)
10-
.add_next(ListNode::new(3).add_next(ListNode::new(4).add_next(ListNode::new(5)))),
11-
),
12-
));
13-
14-
medium::reorder_list::reorder_list(&mut head);
15-
16-
// Print elements of the list
17-
let mut curr = head;
18-
while let Some(node) = curr {
19-
println!("{}", node.val);
20-
curr = node.next;
21-
}
4+
let mut cache = medium::lru_cache::LRUCache::new(2);
5+
cache.put(1, 1);
6+
cache.put(2, 2);
7+
println!("{}", cache.get(1)); // returns 1
8+
cache.put(3, 3); // evicts key 2
9+
println!("{}", cache.get(2)); // returns -1 (not found)
10+
cache.put(4, 4); // evicts key 1
11+
println!("{}", cache.get(1)); // returns -1 (not found)
12+
println!("{}", cache.get(3)); // returns 3
13+
println!("{}", cache.get(4)); // returns 4
2214
}

‎src/medium/lru_cache.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl ListNode {
2323
}
2424
}
2525

26+
/// A struct representing a doubly linked list node.
2627
#[derive(Debug)]
2728
struct DoubleListNode {
2829
head: Option<Rc<RefCell<ListNode>>>,
@@ -202,6 +203,13 @@ impl LRUCache {
202203
}
203204
}
204205

206+
/*
207+
Algorithm - Using a doubly linked list and a hashmap
208+
209+
Time O(1)
210+
Space O(n)
211+
*/
212+
205213
#[cfg(test)]
206214
mod tests {
207215
use super::*;

0 commit comments

Comments
 (0)
Please sign in to comment.