forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUcache.java
117 lines (94 loc) · 3.05 KB
/
LRUcache.java
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
108
109
110
111
112
113
114
115
116
117
//need O(1) time for GET and PUT operations
//Use HashMap for O(1) lookup(GET)
//Use a DLL for O(1) removals/insertions(PUT) , also because WE NEED ORDERING , hashmap will have no ordering by itself!
class LRUCache {
private class ListNode { //definition of Node needed for this problem
int key;
int value;
ListNode prev;
ListNode next;
}
Map<Integer, ListNode> hm = new HashMap<>(); //give us O(1) access to get an item in dll
ListNode head;
ListNode tail;
int totalItemsInCache; //# of items currently in the cahce
int capacity; //items the cache can hold
public LRUCache(int capacity) {
// Cache starts empty and capacity is set by client
totalItemsInCache = 0;
this.capacity = capacity;
// Dummy head and tail nodes to avoid empty states
head = new ListNode();
tail = new ListNode();
// Wire the head and tail together
head.next = tail;
tail.prev = head;
}
public int get(int key) {
ListNode node = hm.get(key);
if(node == null)
{
return -1;
}
moveToHead(node); // item has been accessed, move to front
return node.value;
}
public void put(int key, int value) {
ListNode node = hm.get(key);
if(node == null)
{
ListNode newNode = new ListNode();
newNode.key = key;
newNode.value = value;
// Add to the hashtable and the actual list that represents the cache
hm.put(key, newNode);
addToFront(newNode);
totalItemsInCache++;
// If over capacity remove the LRU item
if (totalItemsInCache > capacity) {
removeLRUEntry();
}
}
else{
node.value=value;
moveToHead(node);
}
}
private void removeLRUEntry() {
ListNode tail = popTail();
hashtable.remove(tail.key);
--totalItemsInCache;
}
private ListNode popTail() {
ListNode tailItem = tail.prev;
removeFromList(tailItem);
return tailItem;
}
private void addToFront(ListNode node) {
// Wire up the new node being to be inserted
node.prev = head;
node.next = head.next;
/*
Re-wire the node after the head. Our node is still sitting "in the middle of nowhere".
We got the new node pointing to the right things, but we need to fix up the original
head & head's next.
head <-> head.next <-> head.next.next <-> head.next.next.next <-> ...
^ ^
|- new node -|
That's where we are before these next 2 lines.
*/
head.next.prev = node;
head.next = node;
}
//just helder method to remove a node from linkedlist
private void removeFromList(ListNode node) {
ListNode savedPrev = node.prev;
ListNode savedNext = node.next;
savedPrev.next = savedNext;
savedNext.prev = savedPrev;
}
private void moveToHead(ListNode node) {
removeFromList(node);
addToFront(node);
}
}