-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC146.java
87 lines (76 loc) · 1.86 KB
/
LC146.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
// problem: 146. LRU Cache. Least Recently Used
// difficulty: medium
// runtime: 36ms, beats 98.99%
// memory: 69%
import java.util.LinkedHashMap;
class ListNode {
int key;
int value;
ListNode prev;
ListNode next;
public ListNode(int k, int val) {
key = k;
value = val;
}
}
class LRUCache {
private int[] map;
private ListNode[] value;
private ListNode head;
private ListNode tail;
private int cap;
private int mapSize;
public LRUCache(int capacity) {
map = new int[10001];
value = new ListNode[10001];
head = new ListNode(0,0);
tail = new ListNode(0,0);
cap = capacity;
head.next = tail;
tail.prev = head;
mapSize = 0;
}
public int get(int key) {
if (map[key] > 0) {
ListNode node = value[key];
remove(node);
insert(node);
return node.value;
} else {
return -1;
}
}
public void put(int key, int val) {
if (map[key] > 0) {
remove(value[key]);
mapSize--;
}
if (mapSize == cap) {
remove(tail.prev);
mapSize--;
}
insert(new ListNode(key,val));
mapSize++;
}
private void remove(ListNode node) {
map[node.key] = 0;
value[node.key] = null;
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void insert(ListNode node) {
map[node.key]++;
value[node.key] = node;
ListNode next = head.next;
head.next = node;
node.prev = head;
next.prev = node;
node.next = 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);
*/