forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_146.java
171 lines (145 loc) · 6.03 KB
/
_146.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 146. LRU Cache
*
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present.
When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache(2);//capacity
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
*/
public class _146 {
public class Solution1 {
public class LRUCache {
/**
* The shortest implementation is to use LinkedHashMap:
* specify a size of the LinkedHashMap;
* override the removeEldestEntry method when its size exceeds max size:
* https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#removeEldestEntry-java.util.Map.Entry-
* in the constructor, set the last boolean variable to be true: it means the ordering mode,
* if we set it to be true, it means in access order, false, means it's in insertion order:
* https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#LinkedHashMap-int-float-boolean-
*/
private Map<Integer, Integer> cache;
private final int max;
public LRUCache(int capacity) {
max = capacity;
cache = new LinkedHashMap<Integer, Integer>(capacity, 1.0f, true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return cache.size() > max;
}
};
}
public int get(int key) {
return cache.getOrDefault(key, -1);
}
public void put(int key, int value) {
cache.put(key, value);
}
}
}
public class Solution2 {
public class LRUCache {
/**
* The more verbose solution is to write a doubly linked list plus a map.
*/
private class Node {
int key;
int value;
LRUCache.Node prev;
LRUCache.Node next;
Node(int k, int v) {
this.key = k;
this.value = v;
}
Node() {
this.key = 0;
this.value = 0;
}
}
private int capacity;
private int count;
private LRUCache.Node head;
private LRUCache.Node tail;
private Map<Integer, LRUCache.Node> map;
// ATTN: the value should be Node type! This is the whole point of having a class called Node!
public LRUCache(int capacity) {
this.capacity = capacity;
this.count = 0;// we need a count to keep track of the number of elements in the cache so
// that we know when to evict the LRU one from the cache
this.map = new HashMap();
head = new LRUCache.Node();
tail = new LRUCache.Node();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
LRUCache.Node node = map.get(key);
// HashMap allows value to be null, this is superior than HashTable!
if (node == null) {
return -1;
} else {
/**Do two operations: this makes the process more clear:
* remove the old node first, and then
* just add the node again.
* This will guarantee that this node will be at the latest position:
* the most recently used position.*/
remove(node);
add(node);
return node.value;
}
}
public void put(int key, int value) {
LRUCache.Node node = map.get(key);
if (node == null) {
node = new LRUCache.Node(key, value);
map.put(key, node);
add(node);
count++;
if (count > capacity) {
/** ATTN: It's tail.prev, not tail, because tail is always an invalid node, it
doesn't contain anything, it's always the tail.prev that is the last node in the
cache*/
LRUCache.Node toDelete = tail.prev;
map.remove(toDelete.key);
remove(toDelete);
count--;
}
} else {
remove(node);
node.value = value;
add(node);
}
}
private void remove(LRUCache.Node node) {
LRUCache.Node next = node.next;
LRUCache.Node prev = node.prev;
prev.next = next;
next.prev = prev;
}
private void add(LRUCache.Node node) {
// ATTN: we'll always add the node into the first position: head.next!!!!
LRUCache.Node next = head.next;
head.next = node;
node.next = next;
node.prev = head;
next.prev = node;
}
}
}
}