Skip to content

Commit 2cbed84

Browse files
committed
Refactors some of the readme files.
1 parent 955bae2 commit 2cbed84

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

LRU Cache/Readme.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# LRU Cache
22

3-
[Least Recently Used][1] (LRU) is a popular algorithm in cache design. Here, we will try to write this algorithm ourselves.
4-
It should support the 2 operations: `get(key)` and `set(key, value)`.
3+
Caches are used to hold objects in memory. A caches size is finite; If the system doesn't have enough memory, the cache must be purged or the program will crash. [Least Recently Used][1] (LRU) is a popular algorithm in cache design. The idea is simple: In low memory situations, the oldest used member of the cache will be purged. A *priority queue* is used to enforce this behavior.
54

6-
## Solution
7-
The key point in this algorithm is how to maintain a priority queue when we do `get` and `set`. Let’s say we have `priority` list, the priority is ranked from high to low. So, head has the highest priority and tail has the lowest priority.
5+
## The priority queue
86

9-
There will be 2 key operations related to this queue.
10-
* Remove element
11-
* Insert element
7+
The key to the LRU cache is the priority queue. For simplicity, you'll model the queue using a linked list. All interactions with the LRU cache should respect this queue; Calling `get` and `set` should update the priority queue to reflect the most recently accessed element.
8+
9+
10+
### Operations
1211

1312
Each time we access an element, either `set` or `get` we need to insert the element in the head of priority list. The `insert` operation will be look like this.
1413

@@ -19,7 +18,7 @@ private func insert(_ key: KeyType, val: Any) {
1918
guard let first = priority.first else {
2019
return
2120
}
22-
key2node[key]() = first
21+
key2node[key] = first
2322
}
2423
```
2524

0 commit comments

Comments
 (0)