-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-almost-equal-pairs-i.cpp
99 lines (96 loc) · 3.11 KB
/
count-almost-equal-pairs-i.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Time: O(n * l^2)
// Space: O(n)
// freq table, combinatorics
class Solution {
public:
int countPairs(vector<int>& nums) {
static const int L = 7;
vector<int> POW10(L);
POW10[0] = 1;
for (int i = 0; i + 1 < L; ++i) {
POW10[i+1] = POW10[i] * 10;
}
unordered_map<int, int> cnt1;
for (const auto& x : nums) {
++cnt1[x];
}
unordered_map<int, int> cnt2;
for (const auto& [x, v] : cnt1) {
for (int i = 0; i < L; ++i) {
const int a = x / POW10[i] % 10;
for (int j = i + 1; j < L; ++j) {
const int b = x /POW10[j] % 10;
if (a == b || !cnt1.count(x - a * (POW10[i] - POW10[j]) + b * (POW10[i] - POW10[j]))) {
continue;
}
cnt2[x - a * (POW10[i] - POW10[j]) + b * (POW10[i] - POW10[j])] += v;
}
}
}
int result = 0;
for (const auto& [_, v] : cnt1) {
result += v * (v - 1) / 2;
}
int total = 0;
for (const auto& [x, v] : cnt1) {
total += v * cnt2[x];
}
return result + total / 2;
}
};
// Time: O(n * l^(2 * k)) = O(n * l^2)
// Space: O(n + l^(2 * k)) = O(n + l^2) = O(n)
// freq table, combinatorics, bfs
class Solution2 {
public:
int countPairs(vector<int>& nums) {
static const int L = 7;
static const int K = 1;
vector<int> POW10(L);
POW10[0] = 1;
for (int i = 0; i + 1 < L; ++i) {
POW10[i+1] = POW10[i] * 10;
}
const auto& at_most = [&](int k, int x) {
unordered_set<int> lookup = {x};
vector<int> result = {x};
for (int u = 0; k; --k) {
for (int v = size(result); u < v; ++u) {
const int x = result[u];
for (int i = 0; i < L; ++i) {
const int a = x / POW10[i] % 10;
for (int j = i + 1; j < L; ++j) {
const int b = x / POW10[j] % 10;
if (a == b) {
continue;
}
const int y = x -a * (POW10[i] - POW10[j]) + b * (POW10[i] - POW10[j]);
if (lookup.count(y)) {
continue;
}
lookup.emplace(y);
result.emplace_back(y);
}
}
}
}
return result;
};
int result = 0;
unordered_map<int, int> cnt1;
for (const auto& x : nums) {
++cnt1[x];
}
unordered_map<int, int> cnt2;
for (const auto& [x, v] : cnt1) {
result += cnt2[x] * v + v * (v - 1) / 2;
for (const auto& x : at_most(K, x)) {
if (!cnt1.count(x)) {
continue;
}
cnt2[x] += v;
}
}
return result;
}
};