Interview Question 16.25. LRU Cache

LRU:Least Recently used

I talked about it in the operating system class. As a whole, a stack is used, and the newly used ones are extracted and put on the top of the stack. If the stack size reaches the limit, delete the bottom of the stack.

Of course, this time complexity is too high, lookup time O (n), update time O (n).

If the time complexity of the above two operations is to reach O (1). Thinking: looking for O (1), that is, there is a key, O (1) time to find whether it exists, certainly the hash table cannot escape.

Also sort by time, with the most recently used first and the oldest used last. However, the node found every time may be in the middle, so dynamic insertion and deletion, that is, a linked list, is required.

 

So overall:

A linked list stores key-value. And arranged according to the appearance time from the most recent to the earliest (the most recent occurrence is at the head of the linked list).

There is also a hash table. The key is the key of the linked list node, and the value is the linked list node item (pointer or iterator) corresponding to the key.

In this way, the corresponding linked list node can be located in O (1) time through the hash table. And when updating, directly move the node to the head of the linked list and update the hash table mapping relationship. This operation is also O (1).

topic:

Design and build a "least recently used" cache that will delete the least recently used items. The cache should map from key to value (allowing you to insert and retrieve the value corresponding to a specific key), and specify the maximum capacity at initialization. When the cache is filled, it should delete the least recently used items.

It should support the following operations: Get data get and write data put.

Get data get (key)-If the key (key) exists in the cache, get the value of the key (always a positive number), otherwise return -1.
Write data put (key, value)-If the key does not exist, write its data value. When the cache capacity reaches the upper limit, it should delete the least recently used data value before writing new data, thereby leaving room for the new data value.

Example:

LRUCache cache = new LRUCache (2 / * cache capacity * /);

cache.put (1, 1);
cache.put (2, 2);
cache.get (1); // returns 1
cache.put (3, 3); // This operation makes the key 2 invalid
. get (2); // returns -1 (not found)
cache.put (4, 4); // this operation makes the key 1 invalid
cache.get (1); // returns -1 (not found)
cache .get (3); // returns 3
cache.get (4); // returns 4

 

answer:

class LRUCache {
public:
    unordered_map<int,list<pair<int,int> >::iterator> mp;
    list<pair<int,int> >data;
    int n;
    LRUCache(int capacity) {
        n=capacity;
    }
    
    int get(int key) {
        if(mp.count(key)){
            put(key,mp[key]->second);
            return mp[key]->second;
        }
         
                data.pop_back ();return - 1 ; 
    } 
    
    void put ( int key, int value) {
         if (mp.count (key)) { 
            auto iter = mp [key]; 
            data.push_front ({key, value}); // Update to the head of the list Department 
            data.erase (iter); // Delete the previous iterator 
            mp [key] = data.begin (); 
        } 
        else {
             if (data.size ()> = n) { 
                auto p = data.back (); 
                mp.erase (p.first); 
            }
            data.push_front({key,value});
            mp[key]=data.begin();
        }
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12741908.html