-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
118 lines (96 loc) · 2.76 KB
/
Copy pathLRUCache.java
File metadata and controls
118 lines (96 loc) · 2.76 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
108
109
110
111
112
113
114
115
116
117
118
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
class MyLRUCache {
private Map<Integer, int[]> map; // int[]{value, frequency}
private Queue<int[]> pQueue; // int[]{key, value, frequency}
private final int capacity;
public MyLRUCache(int capacity) {
map = new HashMap<>();
pQueue = new PriorityQueue<>((num1, num2) -> Integer.compare(num1[2], num2[2]));
this.capacity = capacity;
}
public int get(int key) {
int[] res = map.getOrDefault(key, new int[]{-1});
int answer = res[0];
if (answer != -1) {
map.replace(key, new int[]{map.get(key)[0], ++map.get(key)[1]});
// pQueue.
}
return res[0];
}
public void put(int key, int value) {
if (map.size() >= capacity) {
int[] removePair = pQueue.poll();
map.remove(removePair[0]);
}
map.put(key, new int[]{value, 0});
pQueue.add(new int[]{key, value, 0});
}
}
/**
* 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);
*/
class LRUCache {
class Node {
int key;
int val;
Node prev;
Node next;
Node(int key, int val) {
this.key = key;
this.val = val;
}
}
Node head = new Node(-1, -1);
Node tail = new Node(-1, -1);
int capacity;
HashMap<Integer, Node> map = new HashMap<>();
public LRUCache(int capacity) {
this.capacity = capacity;
head.next = tail;
tail.prev = head;
}
private void addNode(Node newNode) {
Node temp = head.next;
newNode.next = temp;
newNode.prev = head;
head.next = newNode;
temp.prev = newNode;
}
private void deleteNode(Node delNode) {
Node prevv = delNode.prev;
Node nextt = delNode.next;
prevv.next = nextt;
nextt.prev = prevv;
}
public int get(int key) {
if (map.containsKey(key)) {
Node resultNode = map.get(key);
int ans = resultNode.val;
map.remove(key);
deleteNode(resultNode);
addNode(resultNode);
map.put(key, head.next);
return ans;
}
return -1;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
Node curr = map.get(key);
map.remove(key);
deleteNode(curr);
}
if (map.size() == capacity) {
map.remove(tail.prev.key);
deleteNode(tail.prev);
}
addNode(new Node(key, value));
map.put(key, head.next);
}
}