460-最少使用缓存

Description
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.


Follow up:
Could you do both operations in O(1) time complexity?


Example:

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

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.get(3);       // returns 3.
cache.put(4, 4);    // evicts key 1.
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

问题描述

设计并实现一个最少使用频率的数据结构。它需支持get和put操作

  • get(key)-如果键存在,返回值,否则返回-1
  • put(key, value)-若键存在,更新值,否则,插入。当存储的键的数量超过容量,需要在插入键之前将最小使用频率的键删除掉。就这个问题而言,如果有多个键的使用频率一样,那么删除最近最少使用的键。

Follow up:
你能以O(1)的时间复杂度实现两个操作么?


问题分析

首先需要注意,使用频率是针对两个操作的,即get()和put()

思路
维护以下三个数据结构

  • value, 存储键对应值
  • count, 存储键对应的使用频率
  • list, 存储使用频率对应的键的列表

维护两个值,min(最小频率指针), cap(容量)

对于get,若键不存在,返回-1,否则更新count和list,注意在更新的过程中要调整min

对于put,若键存在,调用get更新,若键不存在,需要判断当前存储键的个数是否超过容量,若超过容量,删除min指向的键列表中的第一个键(最早插入的同样频率的列表,第一个就是最近使用频率最少的),更新value,count和list


解法

class LFUCache {
    //store <key, value> pair
    Map<Integer, Integer> value;
    //store <key, count> pair
    Map<Integer, Integer> count;
    //store <count, keylist> pair
    Map<Integer, LinkedHashSet<Integer>> list;
    //least frequent used count
    int min = -1;
    //capacity
    int cap = 0;
    public LFUCache(int capacity) {
        cap = capacity;
        value = new HashMap();
        count = new HashMap();
        list = new HashMap();
        //注意这里
        list.put(1, new LinkedHashSet());
    }

    public int get(int key) {
        //若键不存在,返回-1
        if(!value.containsKey(key))    return -1;

        int cnt = count.get(key);
        //将频率增1
        count.put(key, cnt + 1);
        //删除频率列表中的key
        list.get(cnt).remove(key);
        //若min与cnt相等,说明当前最小使用频率为cnt
        //若该频率对应键值列表为空,那么最小频率增1
        if(min == cnt && list.get(cnt).size() == 0) min++;
        //更新list
        if(!list.containsKey(cnt + 1))  list.put(cnt + 1, new LinkedHashSet());
        list.get(cnt + 1).add(key);

        return value.get(key);
    }

    public void put(int key, int value) {
        if(cap <= 0)    return;
        //若包含key,调用get()进行更新,别忘了更新键对应的值
        if(this.value.containsKey(key)){
            this.value.put(key, value);
            get(key);
            return;
        }
        //若超过容量,删除min对应的键列表的第一个键,并且更新value和count
        if(this.value.size() >= cap){
            int evit = list.get(min).iterator().next();
            list.get(min).remove(evit);
            this.value.remove(evit);
            count.remove(evit);
        }

        this.value.put(key, value);
        //由于插入了一个新元素,当前最小使用频率必然是1
        min = 1;
        count.put(key, 1);
        list.get(1).add(key);
    }
}

/**
 * Your LFUCache object will be instantiated and called as such:
 * LFUCache obj = new LFUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值