forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU-Cache.java
91 lines (78 loc) · 2.02 KB
/
LRU-Cache.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
/**
* LRU Cache
*
* Logic : maintain doubly linked List for insertion at head and deletion from tail and maintain hashmap of key as key and value as node. Node consists of key, value, prev and next pointers.
* Runtime: 16 ms
* Memory Usage: 47.9 MB
*/
class LRUCache {
final Node head = new Node();
final Node tail = new Node();
int capacity;
Map<Integer, Node> map;
public LRUCache(int capacity) {
map = new HashMap(capacity);
this.capacity = capacity;
head.next = tail;
tail.prev = head;
}
public int get(int key) {
int result = -1;
Node node = map.get(key);
if(node!=null)
{
remove(node);
add(node);
result = node.val;
}
return result;
}
public void put(int key, int value) {
Node node = map.get(key);
if(node!=null)
{
remove(node);
node.val = value;
add(node);
}
else{
if(map.size() == capacity)
{
map.remove(tail.prev.key);
remove(tail.prev);
}
Node new_node = new Node();
new_node.key = key;
new_node.val = value;
map.put(key, new_node);
add(new_node);
}
}
public void add(Node node)
{
Node head_next = head.next;
node.next = head_next;
head_next.prev = node;
head.next = node;
node.prev = head;
}
public void remove(Node node)
{
Node next_node = node.next;
Node prev_node = node.prev;
next_node.prev = prev_node;
prev_node.next = next_node;
}
class Node{
int key;
int val;
Node prev;
Node next;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/