解法一 - Hash Table + balanced binary search tree (C++ 裡的 set)
struct CacheNode {
int key;
int value;
int freq;
long tick;
bool operator < (const CacheNode& rhs) const {
if(freq < rhs.freq) return true;
if(freq == rhs.freq) return tick < rhs.tick;
return false;
}
};
class LFUCache {
public:
LFUCache(int capacity) {
capacity_ = capacity;
tick_ = 0;
}
int get(int key) {
auto it = m_.find(key);
if(it == m_.end()) { return -1; }
touch(it->second);
return it->second.value;
}
void put(int key, int value) {
if(capacity_ == 0) {
return;
}
auto it = m_.find(key);
if(it != m_.end()) {
it->second.value = value;
touch(it->second);
return;
}
if(m_.size() == capacity_) {
CacheNode node = *cache_.begin();
m_.erase(node.key);
cache_.erase(node);
}
CacheNode node{key, value, 1, ++tick_};
m_[key] = node;
cache_.insert(node);
}
private:
void touch(CacheNode& node) {
cache_.erase(node);
++node.freq;
node.tick = ++tick_;
cache_.insert(node);
}
long tick_;
int capacity_;
unordered_map<int, CacheNode> m_;
set<CacheNode> cache_;
};
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache* obj = new LFUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/