Skip to content

Commit

Permalink
Aug30: daily problem
Browse files Browse the repository at this point in the history
Sliding window will exceed time limit
  • Loading branch information
aucker committed Aug 30, 2024
1 parent 6428ba9 commit e724566
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cpp/LFU-LRU.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bits/stdc++.h>
#include <unordered_map>
#include <vector>
using namespace std;

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions daily/Aug30.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <vector>
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<int>& nums) {
long long len = nums.size(), m = to_string(nums[0]).length();
long long ans = m * len * (len - 1) / 2;
vector<array<int, 10>> cnt(m);
for (int x : nums) {
for (int i = 0; x; x /= 10) {
ans -= cnt[i++][x %= 10]++;
}
}

return ans;
}

};

0 comments on commit e724566

Please sign in to comment.