diff --git a/cpp/LFU-LRU.cpp b/cpp/LFU-LRU.cpp index c58cf21..70d45ad 100644 --- a/cpp/LFU-LRU.cpp +++ b/cpp/LFU-LRU.cpp @@ -1,4 +1,5 @@ -#include +#include +#include using namespace std; /** @@ -90,7 +91,11 @@ class LFUCache { void put(int key, int value) { if (size == 0) return; if (get(key) != -1) { - cache[key]->val = value; + Node* node = cache[key]; + node->val = value; + node->freq++; + deleteNode(node); + addHead(node); } else { if (cache.size() == size) { popTail(); @@ -160,7 +165,7 @@ class LRUCache { node->prev->next = node->next; } - Node* popTail() { + void popTail() { Node* node = tail->prev; deleteNode(node); cache.erase(node->key); diff --git a/daily/Aug30.cc b/daily/Aug30.cc new file mode 100644 index 0000000..719c054 --- /dev/null +++ b/daily/Aug30.cc @@ -0,0 +1,26 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief LC3153: Sum of digit diffs of all pairs + * Time: O(NlogU), Space: O(DlogU), D = 10 + * + * @param nums + * @return long long + */ + long long sumDigitDifferences(vector& nums) { + long long len = nums.size(), m = to_string(nums[0]).length(); + long long ans = m * len * (len - 1) / 2; + vector> cnt(m); + for (int x : nums) { + for (int i = 0; x; x /= 10) { + ans -= cnt[i++][x %= 10]++; + } + } + + return ans; + } + +};