2022.03.18 - NC071.BM101 设计LFU缓存结构

1. 题目

在这里插入图片描述

2. 思路

(1) 哈希表+优先队列

  • 哈希表count存储key和调用该key的次数和时间,哈希表data存储真正的数据,优先队列存储key,按照调用次数从小到大排序,调用次数相等时,按照调用时间从小到大排序。

3. 代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.PriorityQueue;

public class Solution {
    class Node {
        public int count;
        public int time;

        public Node(int count, int time) {
            this.count = count;
            this.time = time;
        }
    }

    /**
     * lfu design
     *
     * @param operators int整型二维数组 ops
     * @param k         int整型 the k
     * @return int整型一维数组
     */
    public int[] LFU(int[][] operators, int k) {
        HashMap<Integer, Node> count = new HashMap<>();
        HashMap<Integer, Integer> data = new HashMap<>();
        PriorityQueue<Integer> minHeap = new PriorityQueue<>((o1, o2) -> Integer.compare(count.get(o1).count, count.get(o2).count) == 0 ?
                Integer.compare(count.get(o1).time, count.get(o2).time) : Integer.compare(count.get(o1).count, count.get(o2).count));
        ArrayList<Integer> list = new ArrayList<>();
        int time = 0;
        for (int[] operator : operators) {
            if (operator[0] == 1) {
                int key = operator[1];
                int value = operator[2];
                if (data.size() == k) {
                    int removeKey = minHeap.poll();
                    count.remove(removeKey);
                    data.remove(removeKey);
                }
                data.put(key, value);
                if (count.containsKey(key)) {
                    Node node = new Node(count.get(key).count + 1, time++);
                    count.put(key, node);
                    minHeap.remove(key);
                    minHeap.offer(key);
                } else {
                    Node node = new Node(1, time++);
                    count.put(key, node);
                    minHeap.offer(key);
                }
            } else {
                int key = operator[1];
                if (data.containsKey(key)) {
                    list.add(data.get(key));
                    if (count.containsKey(key)) {
                        Node node = new Node(count.get(key).count + 1, time++);
                        count.put(key, node);
                        minHeap.remove(key);
                        minHeap.offer(key);
                    } else {
                        Node node = new Node(1, time++);
                        count.put(key, node);
                        minHeap.offer(key);
                    }
                } else {
                    list.add(-1);
                }
            }
        }
        int n = list.size();
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值