diff --git a/data-structure/number-containers.cpp b/data-structure/number-containers.cpp new file mode 100644 index 0000000..f737a30 --- /dev/null +++ b/data-structure/number-containers.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +/* + The number container system can do the following: + + 1. insert or replace a number at the given index + 2. return the smallest index for the given number in the system + +*/ + +class NumberContainers { +private: + std::unordered_map nums; + std::unordered_map, std::greater>> heap; +public: + NumberContainers() = default; + + void change(int index, int number) { + nums[index] = number; + heap[number].push(index); + } + + int find(int number) { + while (!heap[number].empty() && nums[heap[number].top()] != number) { + heap[number].pop(); + } + + if (heap[number].empty()) { + return -1; + } + return heap[number].top(); + } +}; diff --git a/data-structure/priority-manager.cpp b/data-structure/priority-manager.cpp new file mode 100644 index 0000000..4be761f --- /dev/null +++ b/data-structure/priority-manager.cpp @@ -0,0 +1,48 @@ +#include +#include + +/* + Task scheduling with changeable priorities +*/ + +class PriorityManager { +private: + std::unordered_map info; + std::priority_queue> heap; +public: + PriorityManager(std::vector>& tasks) { + for (auto& task : tasks) { + int taskId = task.first, priority = task.second; + info[taskId] = priority; + heap.push(std::make_pair(priority, taskId)); + } + } + + void add(int taskId, int priority) { + info[taskId] = priority; + heap.push(std::make_pair(priority, taskId)); + } + + void edit(int taskId, int newPriority) { + info[taskId] = newPriority; + heap.push(std::make_pair(newPriority, taskId)); + } + + void rmv(int taskId) { + info.erase(taskId); + } + + int execTop() { + while (!heap.empty()) { + auto task = heap.top(); + heap.pop(); + int priority = task.first, taskId = task.second; + + if (info.find(taskId) != info.end() && info[taskId] == priority) { + rmv(taskId); + return taskId; + } + } + return -1; + } +}; \ No newline at end of file diff --git a/segment-tree/3479 fruits-into-baskets-III.cpp b/segment-tree/3479. fruits-into-baskets-III.cpp similarity index 96% rename from segment-tree/3479 fruits-into-baskets-III.cpp rename to segment-tree/3479. fruits-into-baskets-III.cpp index 8aaf6ea..94415b4 100644 --- a/segment-tree/3479 fruits-into-baskets-III.cpp +++ b/segment-tree/3479. fruits-into-baskets-III.cpp @@ -8,6 +8,12 @@ Return the number of fruit types that remain unplaced after all possible allocations are made. */ +#include +#include +#include + +using namespace std; + class SegmentTree { private: vector segVal;