栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录

1. 题目2. 思路

(1) 哈希表+优先队列 3. 代码 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;
        }
    }

    
    public int[] LFU(int[][] operators, int k) {
        HashMap count = new HashMap<>();
        HashMap data = new HashMap<>();
        PriorityQueue 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 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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号