File tree 2 files changed +34
-3
lines changed
2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change 1
- #include < bits/stdc++.h>
1
+ #include < unordered_map>
2
+ #include < vector>
2
3
using namespace std ;
3
4
4
5
/* *
@@ -90,7 +91,11 @@ class LFUCache {
90
91
void put (int key, int value) {
91
92
if (size == 0 ) return ;
92
93
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);
94
99
} else {
95
100
if (cache.size () == size) {
96
101
popTail ();
@@ -160,7 +165,7 @@ class LRUCache {
160
165
node->prev ->next = node->next ;
161
166
}
162
167
163
- Node* popTail () {
168
+ void popTail () {
164
169
Node* node = tail->prev ;
165
170
deleteNode (node);
166
171
cache.erase (node->key );
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments