-
Notifications
You must be signed in to change notification settings - Fork 3
/
2376.cpp
63 lines (59 loc) · 1.75 KB
/
2376.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
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
// [0, 10 ^ n)
vector<int> dp(n + 1, 0);
dp[0] = 1;
if (n == 0) return dp[0];
dp[1] = 9;
if (n == 1) return dp[0] + dp[1];
int multiply = 9;
for (int i = 2; i <= n; ++i) {
dp[i] = dp[i - 1] * multiply;
multiply--;
}
int total = 0;
for (int i = 0; i <= n; ++i) total += dp[i];
return total;
}
int multiply(int start, int length) {
int res = 1;
for (int i = 0; i < length; ++i) {
res *= start;
start--;
}
return res;
}
bool isUnique(int n) {
string nStr = to_string(n);
vector<bool> used(10, false);
for (auto c : nStr) {
if (used[c - '0']) return false;
used[c - '0'] = true;
}
return true;
}
int countSpecialNumbers(int n) {
string nStr = to_string(n);
int length = nStr.size();
// [0, 10^m)
int uniques = countNumbersWithUniqueDigits(length - 1);
uniques--; // delete 0
// [10^m + 1, n)
vector<bool> used(10, false);
uniques += (nStr[0] - '0' - 1) * multiply(9, length - 1);
used[nStr[0] - '0'] = true;
for (int i = 1; i < length; ++i) {
int usedCnt = 0;
for (int j = 0; j < (nStr[i] - '0'); ++j) {
if (used[j]) usedCnt++;
}
uniques += (nStr[i] - '0' - usedCnt) * multiply(9 - i, length - 1 - i);
if (used[nStr[i] - '0']) break;
used[nStr[i] - '0'] = true;
}
// n
if (isUnique(n)) uniques++;
return uniques;
}
};