Catalogue
Design + Operatin System + Data Structure
OrderedDict
An OrderedDict is a dict that remembers the order that keys were first inserted.1
2
3
4cache = collections.OrderedDict()
cache.popitem(last=True)
# The popitem() method for ordered dictionaries returns and removes a (key, value) pair.
# The pairs are returned in LIFO order if last is true or FIFO order if false.
146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache.
- we need to be able to add an entry to the cache and delete the oldest entry from the cache.(including when the cache is full)
- we need to find out quickly whether an entry is in the cache or not.
- hash map :
- we need to adjust the priority efficiently of each entry in the cache.
- double linked list or singly linked list node
Solution1. Built-In
- python built-in datastructure OrderedDict!!! record the order when they put in the dictionary
1 | class LRUCache(object): |
Solution2. Doublely Linked List + Hash map
- Key Point
- DummyNode, head and tail
- helper function, remove and addHead
1 | class Node(object): |
460. LFU Cache
TODO!!!