-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-distinct-integers-after-removing-zeros.cpp
More file actions
52 lines (49 loc) · 1.2 KB
/
count-distinct-integers-after-removing-zeros.cpp
File metadata and controls
52 lines (49 loc) · 1.2 KB
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
// Time: O(logn)
// Space: O(1)
// combinatorics
class Solution {
public:
long long countDistinct(long long n) {
const auto& reverse = [](int64_t n) {
int64_t result = 0, base = 1;
for (; n; n /= 10, base *= 9) {
result = result * 10 + n % 10;
}
return pair(result, base);
};
auto [m, base] = reverse(n + 1);
int64_t result = (base - 9) / (9 - 1);
base /= 9;
for (; base; base /= 9, m /= 10) {
const int r = m % 10;
if (r == 0) {
break;
}
result += (r - 1) * base;
}
return result;
}
};
// Time: O(logn)
// Space: O(logn)
// combinatorics
class Solution2 {
public:
long long countDistinct(long long n) {
const auto& s = to_string(n);
int64_t base = pow(9, size(s));
int64_t result = (base - 9) / (9 - 1);
base /= 9;
for (const auto& x : s) {
if (x == '0') {
break;
}
result += ((x - '0') - 1) * base;
base /= 9;
}
if (base == 0) {
++result;
}
return result;
}
};