bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 146. LRU Cache

146. LRU Cache

Leetcode Solutions

Ordered Dictionary Approach for LRU Cache

  1. Initialize an ordered dictionary to store cache items.
  2. For the get operation, return the value associated with the key if it exists, and move the key to the end to mark it as recently used. If the key does not exist, return -1.
  3. For the put operation, insert the key-value pair into the dictionary. If the key already exists, update the value and move the key to the end. If adding a new key exceeds the capacity, remove the first key-value pair from the dictionary.
  4. To remove the least recently used item, pop the first item from the ordered dictionary.
UML Thumbnail

Hashmap + DoubleLinkedList Approach for LRU Cache

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...