We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This passes all tests while correctly maintaining both map and double linked Node list:
classLRUCache:publicCache{public:LRUCache(intcapacity){cp=capacity;tail=nullptr;head=nullptr;}intget(intkey)override{if(mp.find(key)!=mp.end())returnmp[key]->value;elsereturn-1;}voidset(intkey,intvalue)override{// Search keyautoit=mp.find(key);// Hitif(it!=mp.end())it->second->value=value;// Misselse{if(mp.empty()){mp[key]=newNode(key,value);head=mp[key];tail=head;}elseif(mp.size()==cp){// Remove tailNode*temp=tail->prev;mp.erase(tail->key);tail=temp;tail->next=nullptr;// Add new Node as headmp[key]=newNode(nullptr,head,key,value);head->prev=mp[key];head=mp[key];}else{// Add new Node as headmp[key]=newNode(nullptr,head,key,value);head->prev=mp[key];head=mp[key];}}}};
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Abstract Classes - Polymorphism
You are viewing a single comment's thread. Return to all comments →
This passes all tests while correctly maintaining both map and double linked Node list: