当前位置:网站首页>leetcode - 460. LFU 缓存
leetcode - 460. LFU 缓存
2022-06-11 08:28:00 【zmm_mohua】
leetcode - 460. LFU 缓存
题目

代码
#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;
}
}
边栏推荐
- ICML2022有意思的文章
- Use guidelines in constraintlayout to limit the maximum width of controls
- Typescript interface and type alias similarities and differences
- node报错整理
- 【node】npm部分
- Typescript namespace
- 进程间的通信
- Process control: process waiting (recycling child processes)
- Web design and website planning assignment 14 add background music to the video
- Pg/oracle database ASCII code to string custom function
猜你喜欢

Typescript header file usage details

B+超强树,带你知晓MySQL的底层是怎样的结构

Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘

你所不知道的console
![[software tool] the hacker matrix special effect software CMatrix](/img/d3/bbaa3dfd244a37f0f8c6227db37257.jpg)
[software tool] the hacker matrix special effect software CMatrix

结果和目标出入太大?不妨借助目标管理精准直达目标!

(resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output

Anaconda related knowledge supplement (spyder+keras Library)

MySQL advanced features, you can read more about it and meet the interview

JS learning basics document Write write a line of text in the page
随机推荐
Introduction to guava cache usage
不想项目失控?你需要用对项目管理工具
qiao-npms:获取npm包下载量
B+ super tree helps you know the underlying structure of MySQL
这几个小工具也太好用了
Use Jackson's @jsonproperty annotation to add one more field to the property name. Solution to the problem
Bat batch processing separate environment packaging
Zipkin入门
Zookepper===>动物管理员系统
(resolved) pychart debug error -unicode decodeerror: 'UTF-8' codec can't decode byte 0xe8 in position 1023
Record a murder case caused by ignoring the @suppresslint ("newapi") prompt
Swagger study notes
Using flying items to manage by objectives, not being a "headless fly" in the workplace
ActiveMQ simple tutorial, suitable for beginners, learning notes yyds
Sign in system design: how to draw the sign in function
Use of Excel to XML tool of TestLink
Oracle learning (I)
怎么做好项目管理?学会这4个步骤就够了
【node】npm部分
Introduction to database system experiment report answer Experiment 5: database single table query