-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttempt.java
87 lines (72 loc) · 2.18 KB
/
Attempt.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
package techQuestions;
import java.util.*;
public class Attempt {
private DoublyLinkedListNode head;
private DoublyLinkedListNode tail;
static class DoublyLinkedListNode {
int key;
int value;
DoublyLinkedListNode next;
DoublyLinkedListNode prev;
}
public void add(DoublyLinkedListNode node) {
// new node gets added to head!
node.next = head.next;
node.prev = head;
// what to do with old node? we don't
// need to updated it's next pointer
head.next.prev = node;
head.next = node;
}
public void popTail() {
// capacity has been met, pop LRU
tail.prev.prev.next = tail;
tail.prev = tail.prev.prev;
}
public void moveToHead(DoublyLinkedListNode node) {
// must remove this node first
DoublyLinkedListNode prev = node.prev;
DoublyLinkedListNode next = node.next;
prev.next = next;
next.prev = prev;
//let's re-add this node
add(node);
}
static HashMap<Integer, DoublyLinkedListNode> map = new HashMap<>();
public static void main(String[] args) {
Attempt cache = new Attempt(2);
cache.put(1, 1);
cache.put(2, 2);
System.out.println(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
System.out.println("test");
}
int capacity;
int size;
public Attempt(int capacity) {
this.capacity = capacity;
this.size = 0;
}
public int get(int key) {
DoublyLinkedListNode node = map.get(key);
moveToHead(node);
return node.value;
}
public void put(int key, int value) {
if (size == capacity) {
// we must remove least recently used!
popTail();
}
// now we can add this new value
DoublyLinkedListNode node = new DoublyLinkedListNode();
node.value = value;
node.key = key;
add(node);
map.put(key, node);
}
}