Skip to content

Commit 862afff

Browse files
author
FreeTymeKiyan
committed
add mnd update ore solutions
1 parent 3b0f184 commit 862afff

19 files changed

+938
-763
lines changed

src/main/java/com/freetymekiyan/algorithms/level/hard/LRUCache.java

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -24,109 +24,109 @@
2424
*/
2525
class LRUCache {
2626

27-
private final int capacity;
28-
private final Map<Integer, Node> cache;
29-
private final Node head;
30-
private final Node tail;
27+
private final int capacity;
28+
private final Map<Integer, Node> cache;
29+
private final Node head;
30+
private final Node tail;
3131

32-
/**
33-
* Remember capacity.
34-
* Create cache map and doubly linked list.
35-
*/
36-
public LRUCache(int capacity) {
37-
this.capacity = capacity;
38-
cache = new HashMap<>();
39-
head = new Node();
40-
tail = new Node();
41-
head.next = tail;
42-
tail.prev = head;
43-
}
32+
/**
33+
* Remember capacity.
34+
* Create cache map and doubly linked list.
35+
*/
36+
public LRUCache(int capacity) {
37+
this.capacity = capacity;
38+
cache = new HashMap<>();
39+
head = new Node();
40+
tail = new Node();
41+
head.next = tail;
42+
tail.prev = head;
43+
}
4444

45-
/**
46-
* Check key in cache.
47-
* If not in cache, return -1.
48-
* If in cache, get the node, move it to head, return its value.
49-
*/
50-
public int get(int key) {
51-
if (!cache.containsKey(key)) {
52-
return -1;
53-
}
54-
Node node = cache.get(key);
55-
moveToHead(node);
56-
return node.val;
45+
/**
46+
* Check key in cache.
47+
* If not in cache, return -1.
48+
* If in cache, get the node, move it to head, return its value.
49+
*/
50+
public int get(int key) {
51+
if (!cache.containsKey(key)) {
52+
return -1;
5753
}
54+
Node node = cache.get(key);
55+
moveToHead(node);
56+
return node.val;
57+
}
5858

59-
/**
60-
* If key already in cache, only need to update value:
61-
* | Get the node, update its value, move to head.
62-
* If key is not in cache:
63-
* | Create a new node.
64-
* | Add it to the head of list and put it in cache.
65-
* | If cache size exceeds capacity:
66-
* | Get the last node, which is the previous node of tail.
67-
* | Remove it from list by its self-reference.
68-
* | Remove it from cache by its key.
69-
*/
70-
public void set(int key, int value) {
71-
if (cache.containsKey(key)) {
72-
Node node = cache.get(key);
73-
node.val = value;
74-
moveToHead(node);
75-
} else {
76-
Node newNode = new Node(key, value);
77-
addNode(newNode);
78-
cache.put(key, newNode);
79-
if (cache.size() > capacity) {
80-
Node last = tail.prev;
81-
removeNode(last);
82-
cache.remove(last.key);
83-
}
84-
}
59+
/**
60+
* If key already in cache, only need to update value:
61+
* | Get the node, update its value, move to head.
62+
* If key is not in cache:
63+
* | Create a new node.
64+
* | Add it to the head of list and put it in cache.
65+
* | If cache size exceeds capacity:
66+
* | Get the last node, which is the previous node of tail.
67+
* | Remove it from list by its self-reference.
68+
* | Remove it from cache by its key.
69+
*/
70+
public void set(int key, int value) {
71+
if (cache.containsKey(key)) {
72+
Node node = cache.get(key);
73+
node.val = value;
74+
moveToHead(node);
75+
} else {
76+
Node newNode = new Node(key, value);
77+
addNode(newNode);
78+
cache.put(key, newNode);
79+
if (cache.size() > capacity) {
80+
Node last = tail.prev;
81+
removeNode(last);
82+
cache.remove(last.key);
83+
}
8584
}
85+
}
8686

87-
/**
88-
* Remove node from list and add it to head.
89-
*/
90-
private void moveToHead(Node node) {
91-
removeNode(node);
92-
addNode(node);
93-
}
87+
/**
88+
* Remove node from list and add it to head.
89+
*/
90+
private void moveToHead(Node node) {
91+
removeNode(node);
92+
addNode(node);
93+
}
9494

95-
/**
96-
* Remove a node from double linked list.
97-
*/
98-
private void removeNode(Node node) {
99-
node.prev.next = node.next;
100-
node.next.prev = node.prev;
101-
}
95+
/**
96+
* Remove a node from double linked list.
97+
*/
98+
private void removeNode(Node node) {
99+
node.prev.next = node.next;
100+
node.next.prev = node.prev;
101+
}
102102

103-
/**
104-
* Add a node after head.
105-
*/
106-
private void addNode(Node node) {
107-
node.prev = head;
108-
node.next = head.next;
109-
head.next.prev = node;
110-
head.next = node;
111-
}
103+
/**
104+
* Add a node after head.
105+
*/
106+
private void addNode(Node node) {
107+
node.prev = head;
108+
node.next = head.next;
109+
head.next.prev = node;
110+
head.next = node;
111+
}
112112

113-
/**
114-
* Double linked list node.
115-
* With previous node, next node, key, and value.
116-
*/
117-
class Node {
113+
/**
114+
* Double linked list node.
115+
* With previous node, next node, key, and value.
116+
*/
117+
class Node {
118118

119-
Node prev;
120-
Node next;
121-
int key;
122-
int val;
119+
Node prev;
120+
Node next;
121+
int key;
122+
int val;
123123

124-
public Node() {
125-
}
124+
public Node() {
125+
}
126126

127-
public Node(int key, int val) {
128-
this.key = key;
129-
this.val = val;
130-
}
127+
public Node(int key, int val) {
128+
this.key = key;
129+
this.val = val;
131130
}
131+
}
132132
}

0 commit comments

Comments
 (0)