Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

algorithm - LRUCache Design

This question is pertaining to the design involved in building a LRUCache. The complete problem can be found here. Although the solution ends up using a doubly linked list due to best time complexity for adding, deleting and lookup a key value pair, but I was wondering if we could use arrays for the same?

It'll be helpful to think of other data structures such as heaps, binary search trees, heaps as well. Can someone provide their thoughts on the same?

question from:https://stackoverflow.com/questions/65854057/lrucache-design

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For LRUCache, the basic operations include:

  1. get the value of a key, and mark it as the most recently used key
  2. update the existing (key, value) pair
  3. add the new key and its value into the cache, and potentially evict the least recently used key if out of capacity

One common solution is to use doubly linked list to maintain the key usage status (move 1 element to the tail, add 1 element to the tail, remove 1 element from the head all takes O(1)), while using hashmap of (key, pointer of list element) to query and update the key (get, set takes O(1)).

If you replace doubly linked list with array, move 1 element to the tail will take O(n). If you replace doubly linked list with heap or binary search trea, remove 1 element from the head will always take O(log(n)). So they are not good alternatives of doubly linked list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...