From 77375fd9d0f2b84e37bb56ada5e0d86a260b1d42 Mon Sep 17 00:00:00 2001 From: euchangxian Date: Tue, 22 Oct 2024 17:43:40 +0800 Subject: [PATCH] feat(C++): Add 2349 --- .../Solution.cpp | 58 +++++++++++++++++++ README.md | 1 + 2 files changed, 59 insertions(+) create mode 100644 C++/2349-DesignANumberContainerSystem/Solution.cpp diff --git a/C++/2349-DesignANumberContainerSystem/Solution.cpp b/C++/2349-DesignANumberContainerSystem/Solution.cpp new file mode 100644 index 0000000..b9694ca --- /dev/null +++ b/C++/2349-DesignANumberContainerSystem/Solution.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; +/** + * Seems to be a many-to-one relationship between Buckets/Containers and + * Numbers. + * Each Container can store only one Number, but each Number may be stored in + * multiple Containers. + * + * 1 <= index, number <= 10^9. => 4MB of integers possible => unordered_map + * should be used instead of an array. + * + * Naively, seems like two unordered_map is sufficient. + * One for bucket -> number + * The other for number -> {bucket1, ...} + * + * For the number -> buckets, use an ordered Set. + */ +class NumberContainers { + private: + std::unordered_map containers; + std::unordered_map> numbers; + + public: + NumberContainers() {} + + void change(int index, int number) { + auto iter = containers.find(index); + if (iter != containers.end()) { + int original = iter->second; + numbers[original].erase(index); + + if (numbers[original].empty()) { + numbers.erase(original); + } + } + containers[index] = number; + numbers[number].insert(index); + } + + int find(int number) { + auto iter = numbers.find(number); + if (iter == numbers.end()) { + return -1; + } + + return *(iter->second).begin(); + } +}; + +/** + * Your NumberContainers object will be instantiated and called as such: + * NumberContainers* obj = new NumberContainers(); + * obj->change(index,number); + * int param_2 = obj->find(number); + */ diff --git a/README.md b/README.md index 7922a90..1eb4238 100644 --- a/README.md +++ b/README.md @@ -412,6 +412,7 @@ Now solving in C++. Like it. | 2331 | EvaluateBooleanBinaryTree | [![Rust](assets/rust.svg)](Rust/2331-EvaluateBooleanBinaryTree/Solution.rs) [![C++](assets/c++.svg)](C++/2331-EvaluateBooleanBinaryTree/Solution.cpp) | | 2334 | SubarrayWithElementsGreaterThanVaryingThreshold | [![C++](assets/c++.svg)](C++/2334-SubarrayWithElementsGreaterThanVaryingThreshold/Solution.cpp) | | 2336 | SmallestNumberInInfiniteSet | [![C++](assets/c++.svg)](C++/2336-SmallestNumberInInfiniteSet/Solution.cpp) | +| 2349 | DesignANumberContainerSystem | [![C++](assets/c++.svg)](C++/2349-DesignANumberContainerSystem/Solution.cpp) | | 2373 | LargestLocalValuesInAMatrix | [![Rust](assets/rust.svg)](Rust/2373-LargestLocalValuesInAMatrix/Solution.rs) | | 2392 | BuildAMatrixWithConditions | [![C++](assets/c++.svg)](C++/2392-BuildAMatrixWithConditions/Solution.cpp) | | 2393 | CountStrictlyIncreasingSubarrays | [![C++](assets/c++.svg)](C++/2393-CountStrictlyIncreasingSubarrays/Solution.cpp) |