Skip to content

Commit e724566

Browse files
committed
Aug30: daily problem
Sliding window will exceed time limit
1 parent 6428ba9 commit e724566

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

cpp/LFU-LRU.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <bits/stdc++.h>
1+
#include <unordered_map>
2+
#include <vector>
23
using namespace std;
34

45
/**
@@ -90,7 +91,11 @@ class LFUCache {
9091
void put(int key, int value) {
9192
if (size == 0) return;
9293
if (get(key) != -1) {
93-
cache[key]->val = value;
94+
Node* node = cache[key];
95+
node->val = value;
96+
node->freq++;
97+
deleteNode(node);
98+
addHead(node);
9499
} else {
95100
if (cache.size() == size) {
96101
popTail();
@@ -160,7 +165,7 @@ class LRUCache {
160165
node->prev->next = node->next;
161166
}
162167

163-
Node* popTail() {
168+
void popTail() {
164169
Node* node = tail->prev;
165170
deleteNode(node);
166171
cache.erase(node->key);

daily/Aug30.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @brief LC3153: Sum of digit diffs of all pairs
8+
* Time: O(NlogU), Space: O(DlogU), D = 10
9+
*
10+
* @param nums
11+
* @return long long
12+
*/
13+
long long sumDigitDifferences(vector<int>& nums) {
14+
long long len = nums.size(), m = to_string(nums[0]).length();
15+
long long ans = m * len * (len - 1) / 2;
16+
vector<array<int, 10>> cnt(m);
17+
for (int x : nums) {
18+
for (int i = 0; x; x /= 10) {
19+
ans -= cnt[i++][x %= 10]++;
20+
}
21+
}
22+
23+
return ans;
24+
}
25+
26+
};

0 commit comments

Comments
 (0)