-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
107 lines (84 loc) · 2.09 KB
/
Copy pathLRUCache.java
File metadata and controls
107 lines (84 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package practice.lrucache;
import java.util.HashMap;
public class LRUCache {
private class Node {
int key;
int value;
Node prev;
Node next;
Node(int key, int value) {
this.key = key;
this.value = value;
}
}
private final int capacity;
private final HashMap<Integer, Node> map;
private final Node head;
private final Node tail;
public LRUCache(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
// Dummy head, avoiding null checks
head = new Node(0, 0);
tail = new Node(0, 0);
head.next = tail;
tail.prev = head;
}
public int get(int key) {
if (!map.containsKey(key)) return -1;
Node node = map.get(key);
remove(node);
insertToFront(node);
return node.value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
remove(map.get(key));
}
if (map.size() == capacity) {
// Remove least recent node
Node lru = tail.prev;
remove(lru);
}
Node newNode = new Node(key, value);
insertToFront(newNode);
}
// Remove a node from the list and the map
private void remove(Node node) {
map.remove(node.key);
node.prev.next = node.next;
node.next.prev = node.prev;
}
// Insert node right after head (the most recent one)
private void insertToFront(Node node) {
map.put(node.key, node);
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
// Testing
public void printCache() {
Node curr = head.next;
System.out.print("Cache: ");
while (curr != tail) {
System.out.print("(" + curr.key + ":" + curr.value + ") ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
cache.printCache();
cache.get(2);
cache.printCache();
cache.put(4, 40);
cache.printCache();
System.out.println("Get 1: " + cache.get(1));
System.out.println("Get 3: " + cache.get(3));
cache.printCache();
}
}