Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
LRU - Set Sol
#include<bits/stdc++.h> using namespace std; map<int,int> db, timeDb; struct myComp{ bool operator()(int a, int b) { return timeDb[a] > timeDb[b]; } }; set<int,myComp> lru; int ts; int K; // ===================================================================== void LRUCache(int cap) { db.clear(); lru.clear(); timeDb.clear(); K = cap; ts = 0; } //Function to return value corresponding to the key. int GET(int key) { if(lru.find(key) != lru.end()) { auto it = lru.find(key); lru.erase(it); timeDb[key] = ts; ts+=1; lru.insert(key); return db[key]; } return -1; } //Function for storing key-value pair. void SET(int key, int value) { if(lru.find(key) != lru.end()) { auto it = lru.find(key); db[key] = value; lru.erase(it); timeDb[key] = ts; ts+=1; lru.insert(key); } else { db[key] = value; timeDb[key] = ts; ts += 1; if(lru.size() < K) { lru.insert(key); } else { auto it = lru.end(); it--; lru.erase(it); lru.insert(key); } } }
run
|
edit
|
history
|
help
0
Stream7
10 wizards-DFS_vector
int divide by int
QuadRootPoint
Radix sort
basic caculate i
mine
Ternary operator - warnings about unsued values
Dar
Test 7(2020)