Skip to content

Commit

Permalink
Lru cache (#183)
Browse files Browse the repository at this point in the history
* add lru cache

* impl lru cache
  • Loading branch information
SKTT1Ryze authored Mar 5, 2024
1 parent 1dd75e9 commit 47518d5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
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;
}

0 comments on commit 47518d5

Please sign in to comment.