lettcode-LFU缓存

题目描述

请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。它应该支持以下操作:get 和 put。

get(key) - 如果键存在于缓存中,则获取键的值(总是正数),否则返回 -1。
put(key, value) - 如果键不存在,请设置或插入值。当缓存达到其容量时,则应该在插入新项之前,使最不经常使用的项无效。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最近 最少使用的键。

「项的使用次数」就是自插入该项以来对其调用 get 和 put 函数的次数之和。使用次数会在对应项被移除后置为 0

案例

LFUCache cache = new LFUCache( 2 /* capacity (缓存容量) */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回 1
cache.put(3, 3);    // 去除 key 2
cache.get(2);       // 返回 -1 (未找到key 2)
cache.get(3);       // 返回 3
cache.put(4, 4);    // 去除 key 1
cache.get(1);       // 返回 -1 (未找到 key 1)
cache.get(3);       // 返回 3
cache.get(4);       // 返回 4

代码(在本地可以运行成功,但是lettcode上不行)

class LFUCache {
public:
     LFUCache(int capacity) {
	cacheCapacity = capacity;
}
int get(int key) {
	int data = cacheData[key].first;
	if (data == 0) {//删除自动加在map中的元素
		unordered_map<int, pair<int, int>>::iterator it = cacheData.find(key);
		cacheData.erase(it);
		return -1;
	}
	else {
		cacheData[key].second++;
		int frequency = cacheData[key].second;
		//删除之前在频率表中的数据
		unordered_map<int, int>::iterator it = mapData[frequency - 1].find(key);
		mapData[frequency - 1].erase(it);
		if (mapData[frequency - 1].size() == 0) {//删除元素
			map<int, unordered_map<int, int>>::iterator itMy = mapData.find(frequency - 1);
			mapData.erase(itMy);
		}
		//加入新的频率表中
		mapData[frequency][key] = 0;
		return cacheData[key].first;
	}
}
void put(int key, int value) {
	if (cacheData.size() == cacheCapacity) {//删除操作
		/*int index = mapData[0].size() - 1;*/
		unordered_map<int, int>::iterator it = mapData.begin()->second.begin();
		//it--;
		int cacheKey = it->first;//获得要删除的元素的Key
		mapData.begin()->second.erase(it);
		if (mapData.begin()->second.size() == 0) {//删除元素
			map<int, unordered_map<int, int>>::iterator itMy = mapData.begin();
			mapData.erase(itMy);
		}
		//删除元素
		unordered_map<int, pair<int, int>>::iterator itm = cacheData.find(cacheKey);
		cacheData.erase(itm);
	}
	cacheData[key] = { value,0 };
	mapData[0][key] = value;//增加到频率数组中
}


private:
    int cacheCapacity;
    unordered_map<int,pair<int,int>> cacheData;
    map<int,unordered_map<int,int>> mapData;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 Python 实现的 LFU 缓存: ```python from collections import defaultdict class LFUCache: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.freq = defaultdict(OrderedDict) self.min_freq = 0 def get(self, key): if key not in self.cache: return -1 value, freq = self.cache[key] self.freq[freq].pop(key) if len(self.freq[freq]) == 0: self.freq.pop(freq) if self.min_freq == freq: self.min_freq += 1 self.freq[freq+1][key] = value self.cache[key] = (value, freq+1) return value def put(self, key, value): if self.capacity == 0: return if key in self.cache: self.cache[key] = (value, self.cache[key][1]) self.get(key) return if len(self.cache) == self.capacity: k, v = self.freq[self.min_freq].popitem(last=False) self.cache.pop(k) self.cache[key] = (value, 1) self.freq[1][key] = value self.min_freq = 1 ``` 该实现中,LFU 缓存使用了一个字典 `cache` 来存储 key-value 对,使用一个字典 `freq` 来记录每个频次对应的 key-value 对,使用变量 `min_freq` 来记录当前最小的频次。 在 `get` 方法中,如果 key 不存在于 `cache` 中,则返回 -1。否则,将 key 对应的 value 和频次从 `freq` 中删除,如果该频次对应的 key-value 对已经为空,则将该频次从 `freq` 中删除,并更新 `min_freq`。然后,将 key 对应的 value 和频次加一后添加到 `freq` 中,并更新 `cache` 中的 key-value 对。 在 `put` 方法中,如果 `capacity` 为 0,则直接返回。如果 key 已存在于 `cache` 中,则将 `cache` 中的 key-value 对更新,并调用 `get` 方法以更新 `freq` 和 `min_freq`。如果 `cache` 已经满了,则从 `freq` 中删除当前最小频次的 key-value 对,并从 `cache` 中删除对应的 key-value 对。然后,将新的 key-value 对添加到 `cache` 和 `freq` 中,并将 `min_freq` 设置为 1。 使用示例: ```python cache = LFUCache(2) cache.put(1, 1) cache.put(2, 2) assert cache.get(1) == 1 cache.put(3, 3) assert cache.get(2) == -1 assert cache.get(3) == 3 cache.put(4, 4) assert cache.get(1) == -1 assert cache.get(3) == 3 assert cache.get(4) == 4 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值