class LRUCache { public: LRUCache(int capacity) { this->capacity = capacity; } int get(int key) { if(!m.count(key) || m[key] == -1) return -1; q.push_back(key); visited[key]++; return m[key]; } void put(int key, int value) { if(!m.count(key)|| m[key] == -1) count++; else visited[key]++; if(count > capacity){ while(visited[q.front()]) visited[q.front()]--, q.pop_front(); m[q.front()] = -1; q.pop_front(); count--; } q.push_back(key); m[key] = value; } private: unordered_map<int, int>m; unordered_map<int, int>visited; deque<int>q; int count = 0; int capacity = 0; }; /** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */