当前位置:网站首页>leetcode - 460. LFU cache
leetcode - 460. LFU cache
2022-06-11 08:43:00 【zmm_ mohua】
leetcode - 460. LFU cache
subject

Code
#include <iostream>
#include <set>
#include <map>
using namespace std;
struct Node{
int cnt, time, key, value;
bool operator < (const Node& rhs) const {
return cnt == rhs.cnt ? time < rhs.time : cnt < rhs.cnt;
}
};
int capacity, time;
map<int, Node> table;
set<Node> s;
LFUCache(int _capacity) {
capacity = _capacity;
time = 0;
table.clear();
s.clear();
}
int get(int key) {
if(capacity == 0){
return -1;
}
if(table.find(key) == table.end()){
return -1;
}
Node node = table[key];
s.erase(node);
node.cnt += 1;
node.time = ++time;
s.insert(node);
table[key] = node;
return node.value;
}
void put(int key, int value) {
if(capacity == 0){
return;
}
if(table.find(key) == table.end()){
if(s.size() == capacity){
table.erase(s.begin()->key);
s.erase(s.begin());
}
Node node;
node.key = key;
node.value = value;
node.cnt = 1;
node.time = ++time;
table[key] = node;
s.insert(node);
}else{
Node node = table[key];
s.erase(node);
node.cnt += 1;
node.time = ++time;
node.value = value;
s.insert(node);
table[key] = node;
}
}
边栏推荐
- torch. meshgrid
- Installing MySQL and cluster operation on virtual machine in Linux system
- 进程间的通信
- GO语言:字符串的连接、数字转化字符串
- Introduction to database system experiment report answer Experiment 6: advanced query of data table
- The role of lambdalr in pytorch
- Project training - clonemon
- JS basic learning script
- 字符设备驱动程序之异步通知机制
- Typescript namespace
猜你喜欢

Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)

(一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)

SSM file upload and download

go for it Easily manage all types of items with "flying items"

Solve valueerror: no model found in config file

Web design and website planning assignment 11 game selection form

剑指 Offer 10- II. 青蛙跳台阶问题

Redis6 entry-level tutorial. There are integration cases. You can directly see the integration cases. It is easy to get started

Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
随机推荐
Pg/oracle database ASCII code to string custom function
【1】 Integrated learning: quickly understand Integrated Learning
Typescript namespace
MySQL advanced features, you can read more about it and meet the interview
for in / for of / forEach 循环
Web design and website planning assignment 14 add background music to the video
【clickhouse专栏】新建库角色用户初始化
GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
qiao-npms:获取npm包下载量
Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
ActiveMQ简单教程,适合初学者,学习笔记yyds
(the slow download speed of cifar10 in torchvision has been solved) how to download and use torchvision import
AttributeError: module ‘tensorflow. compat. v2.__ internal__‘ has no attribute ‘register_ clear_ session_
Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)
The role of lambdalr in pytorch
Oracle learning (I)
Anaconda related knowledge supplement (spyder+keras Library)
Deep understanding of add in argparse module_ Argument parameters (such as action)
Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘
Introduction to database system experiment report answer Experiment 6: advanced query of data table