Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lru cache #183

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions leetcode-cc/LruCache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <unordered_map>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(PLRUCache, 146, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"LRU Cache",
"Design a data structure that follows the constraints "
"of a Least Recently Used (LRU) cache.",
{"Hash Table & Double Linked List"});

class SLRUCache : public ISolution {
public:
size_t problemId() const override { return 146; }
string name() const override {
return ("Solution for " + string("LRU Cache"));
}
string location() const override { return __FILE_NAME__; }
int test() const override { return 0; };
int benchmark() const override { return 0; }
};

struct DListNode {
int key;
int val;
DListNode* prev;
DListNode* next;
DListNode() : key(-1), val(-1), prev(nullptr), next(nullptr) {}
DListNode(int key, int val)
: key(key), val(val), prev(nullptr), next(nullptr) {}
};

class LRUCache {
public:
LRUCache(int capacity) {
this->capacity = capacity;
this->head = new DListNode();
this->tail = new DListNode();
this->head->next = tail;
this->tail->prev = head;
}

int get(int key) {
if (this->inner.contains(key)) {
auto node = this->inner[key];
this->moveToHead(node);
return node->val;
} else {
return -1;
}
}

void put(int key, int value) {
if (this->inner.contains(key)) {
auto node = this->inner[key];
node->val = value;
this->moveToHead(node);
} else {
if (this->inner.size() >= capacity) {
auto tail = this->removeTail();
this->inner.erase(tail->key);
delete tail;
}
auto node = new DListNode(key, value);
this->inner.insert({key, node});
this->addNodeToHead(node);
}
}

private:
unordered_map<int, DListNode*> inner = {};
DListNode* head;
DListNode* tail;
int capacity;

static void removeNode(DListNode* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}

void addNodeToHead(DListNode* node) {
node->next = this->head->next;
this->head->next->prev = node;
this->head->next = node;
node->prev = this->head;
}

void moveToHead(DListNode* node) {
removeNode(node);
this->addNodeToHead(node);
}

DListNode* removeTail() {
auto node = tail->prev;
removeNode(node);
return node;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "../leetcode-cc/LongestCommonPrefix.hpp"
#include "../leetcode-cc/LongestCommonSubsequence.hpp"
#include "../leetcode-cc/LongestConsecutiveSeq.hpp"
#include "../leetcode-cc/LruCache.hpp"
#include "../leetcode-cc/MaxDepthOfBTree.hpp"
#include "../leetcode-cc/MaxDiffBetNodeAndAncestor.hpp"
#include "../leetcode-cc/MaxLenOfConcatenatedStrWithUniqueChars.hpp"
Expand Down Expand Up @@ -621,5 +622,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
return std::make_shared<SBTreePostorderTraversal>();
});

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PLRUCache>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SLRUCache>(); });

return 0;
}
Loading