146.LRUキャッシングメカニズム

146.LRUキャッシングメカニズム

タイトル説明

知っているデータ構造を使用して、LRU(最近使用されていない)キャッシュメカニズムを設計および実装します。

LRUCacheカテゴリを実現する:

  • LRUCache(int capacity)正の整数のcapacity初期化キャッシュLRUとしての容量

  • int get(int key)キーワードkeyがキャッシュに存在する場合は、キーの値が返されます-1存在しない場合は、が返されます

  • void put(int key, int value)キーワードがすでに存在する場合は、そのデータ値を変更します。キーワードが存在しない場合は、「keyword-value」のグループを挿入します。キャッシュ容量が上限に達すると、新しいデータを書き込む前に最も古い未使用のデータ値を削除して、新しいデータ値のためのスペースを確保する必要があります。

高度O(1)時間計算量内で両方の操作完了できますか?

例:

输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4

促す:

  • 1 <= capacity <= 3000

  • 0 <= key <= 3000

  • 0 <= value <= 10^4

  • ほとんどの3 * 10^4場合電話getしてput


回答:

二重リンクリスト+ハッシュ。

二重リンクリストの優先順位が末尾から先頭に向かって減少すると仮定すると、次のようになります。

  • put(key, value) 最後にノードを挿入します
  • get(key)key存在する場合、これは尾に挿入されます
  • ヘッダーノードを削除すると、ハッシュテーブルkeyレコードから削除および削除できます

時間計算量:O(1)O(1)O 1

追加のスペースの複雑さ:O(容量)O(容量)O c a p a c i t y

class LRUCache {
    
    
public:
    LRUCache(int capacity) : max_size(capacity) {
    
     }
    
    int get(int key) {
    
    
        auto iter = hash_map.find( key );
        if ( iter != hash_map.end() ) {
    
    
            lst.splice( lst.end(), lst, iter->second );
            return iter->second->second;
        } else return -1;
    }
    
    void put(int key, int value) {
    
    
        auto iter = hash_map.find( key );
        if ( iter != hash_map.end() ) {
    
    
            lst.splice( lst.end(), lst, iter->second );
            iter->second->second = value;
        } else {
    
    
            hash_map[key] = lst.insert( lst.end(), {
    
    key, value} );
            if ( lst.size() > max_size ) {
    
    
                hash_map.erase( lst.front().first );
                lst.pop_front();
            }
        }
    }
private:
    using key_value_pair = pair<int, int>;
    using list_iterator = list<key_value_pair>::iterator;

    size_t max_size;
    list<key_value_pair> lst;
    unordered_map<int, list_iterator> hash_map;
};
/*
时间:92ms,击败:95.82%
内存:39MB,击败:71.53%
*/

手書きの二重リンクリストのバージョンを投稿し、簡単な操作のために仮想ヘッドノードとテールノードを追加します。

class DLinkedNode {
    
    
public:
    DLinkedNode( int _key = 0, int _val = 0 ) : key(_key), val(_val), next(nullptr), prev(nullptr) {
    
     }
private:
    int key, val;
    DLinkedNode *next, *prev;
    friend class LRUCache;
};

class LRUCache {
    
    
public:
    void _insert (int key, int value) {
    
    
        DLinkedNode *node = new DLinkedNode( key, value );

        node->next = tail;
        node->prev = tail->prev;
        tail->prev->next = node;
        tail->prev = node;

        hash[key] = node;
        ++size;
    }

    void _delete () {
    
    
        auto node = head->next;
        head->next = node->next;
        node->next->prev = head;
        hash.erase( node->key );
        delete node;
        --size;
    }

    void remove( DLinkedNode* node ) {
    
    
        node->prev->next = node->next;
        node->next->prev = node->prev;

        node->next = tail;
        node->prev = tail->prev;
        node->prev->next = node;
        tail->prev = node;
    }

    LRUCache(int capacity) : max_size(capacity), size(0) {
    
     
        head = new DLinkedNode();
        tail = new DLinkedNode();
        head->next = tail;
        tail->prev = head;
    }
    
    int get(int key) {
    
    
        auto iter = hash.find( key );
        if ( iter == hash.end() ) return -1;
        else {
    
    
            remove( iter->second );
            return iter->second->val;
        }
    }
    
    void put(int key, int value) {
    
    
        auto iter = hash.find( key );
        if ( iter == hash.end() ) {
    
    
            _insert( key, value );
            if ( size > max_size ) _delete();
        } else {
    
    
            iter->second->val = value;
            remove( iter->second );
        }
    }
private:
    DLinkedNode *head, *tail;
    int max_size, size;
    unordered_map<int, DLinkedNode*> hash;
};
/*
时间:88ms,击败:97.84%
内存:38.8MB,击败:94.56%
*/

おすすめ

転載: blog.csdn.net/MIC10086/article/details/114179476