对力扣460题,LFU缓存问题的看法

力扣 460.LFU 缓存

题目链接

题目要求

请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。

实现 LFUCache 类:

①:LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象
②:int get(int key) - 如果键 key 存在于缓存中,则获取键的值,否则返回 -1 。
③:void put(int key, int value) - 如果键 key 已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量 capacity 时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最近最久未使用 的键。

为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。
当一个键首次插入到缓存中时,它的使用计数器被设置为 1 (由于 put 操作)。对缓存中的键执行 get 或 put 操作,使用计数器的值将会递增。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

自己的答案

未通过原因:超时

public class LFUCache {
    //利用map作为容器,但好像时间复杂度过高
    private HashMap<Integer, StoreObject> map;
    private Integer capacity;
    //这个对象的初始时间,以后每进行一次操作,
    //就将时间+1,从而弥补计算机执行过快而导致的系统时间重复
    private Integer initalTime=0;
    public LFUCache(int capacity) {
        map = new HashMap<>(capacity);
        this.capacity = capacity;
    }

    public int get(int key) {
        initalTime++;
        if (map.containsKey(key)) {
            StoreObject storeObject = map.get(key);
            storeObject.setTime(initalTime);
            storeObject.setCounter(storeObject.getCounter() + 1);
            return storeObject.getValue();
        }
        return -1;
    }

    public void put(int key, int value) {
        initalTime++;
        if (map.containsKey(key)) {
            map.put(key, new StoreObject(map.get(key).getCounter()+1, initalTime, value));
        } else {
            if (this.capacity == map.size()) {
                // 判断最少执行次数
                Integer minCounter = 200000;
                Set<Integer> keySet = new HashSet<>();
                for (Map.Entry<Integer, StoreObject> entry : map.entrySet()) {
                    if (entry.getValue().getCounter() == minCounter) {
                        keySet.add(entry.getKey());
                    } else if (entry.getValue().getCounter() < minCounter) {
                        minCounter = entry.getValue().getCounter();
                        keySet.clear();
                        keySet.add(entry.getKey());
                    }
                }
                // 判断最久空置时间
                Integer minTime=200000;
                if (keySet.size() > 1) {
                    Iterator<Integer> iterator = keySet.iterator();
                    Integer minTimeKey = null;
                    while (iterator.hasNext()) {
                        Integer next = iterator.next();
                        Integer time = map.get(next).getTime();
                        if (time < minTime) {
                            minTime = time;
                            minTimeKey = next;
                        }
                    }
                    map.remove(minTimeKey);
                } else {
                    Iterator<Integer> iterator = keySet.iterator();
                    Integer minCounterKey = iterator.next();
                    map.remove(minCounterKey);
                }
            }
            map.put(key, new StoreObject(1, initalTime, value));
        }
    }
}
//用于存储计数器和时间
class StoreObject {
    private Integer counter;
    private Integer time;

    public StoreObject(Integer counter, Integer time, Integer value) {
        this.counter = counter;
        this.time = time;
        this.value = value;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    private Integer value;

    public StoreObject() {
        this.counter = 0;
        this.time = 0;
    }

    public Integer getCounter() {
        return counter;
    }

    public void setCounter(Integer counter) {
        this.counter = counter;
    }

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }
}

力扣官方的题解

class LFUCache {
    // 缓存容量,时间戳
    int capacity, time;
    Map<Integer, Node> key_table;
    TreeSet<Node> S;

    public LFUCache(int capacity) {
        this.capacity = capacity;
        this.time = 0;
        key_table = new HashMap<Integer, Node>();
        S = new TreeSet<Node>();
    }
    
    public int get(int key) {
        if (capacity == 0) {
            return -1;
        }
        // 如果哈希表中没有键 key,返回 -1
        if (!key_table.containsKey(key)) {
            return -1;
        }
        // 从哈希表中得到旧的缓存
        Node cache = key_table.get(key);
        // 从平衡二叉树中删除旧的缓存
        S.remove(cache);
        // 将旧缓存更新
        cache.cnt += 1;
        cache.time = ++time;
        // 将新缓存重新放入哈希表和平衡二叉树中
        S.add(cache);
        key_table.put(key, cache);
        return cache.value;
    }
    
    public void put(int key, int value) {
        if (capacity == 0) {
            return;
        }
        if (!key_table.containsKey(key)) {
            // 如果到达缓存容量上限
            if (key_table.size() == capacity) {
                // 从哈希表和平衡二叉树中删除最近最少使用的缓存
                key_table.remove(S.first().key);
                S.remove(S.first());
            }
            // 创建新的缓存
            Node cache = new Node(1, ++time, key, value);
            // 将新缓存放入哈希表和平衡二叉树中
            key_table.put(key, cache);
            S.add(cache);
        } else {
            // 这里和 get() 函数类似
            Node cache = key_table.get(key);
            S.remove(cache);
            cache.cnt += 1;
            cache.time = ++time;
            cache.value = value;
            S.add(cache);
            key_table.put(key, cache);
        }
    }
}

class Node implements Comparable<Node> {
    int cnt, time, key, value;

    Node(int cnt, int time, int key, int value) {
        this.cnt = cnt;
        this.time = time;
        this.key = key;
        this.value = value;
    }

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof Node) {
            Node rhs = (Node) anObject;
            return this.cnt == rhs.cnt && this.time == rhs.time;
        }
        return false;
    }

    public int compareTo(Node rhs) {
        return cnt == rhs.cnt ? time - rhs.time : cnt - rhs.cnt;
    }

    public int hashCode() {
        return cnt * 1000000007 + time;
    }
}

两者的区别

力扣官方采用了平衡二叉树来维持平衡,这样就使得在删除缓存的时候不需要再去遍历,从而减小了时间复杂度
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值