-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
31 lines (31 loc) · 999 Bytes
/
Solution.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
class Solution {
public:
int numDupDigitsAtMostN(int n) {
string s = to_string(n);
int m = s.size();
int f[m][1 << 10];
memset(f, -1, sizeof(f));
auto dfs = [&](this auto&& dfs, int i, int mask, bool lead, bool limit) -> int {
if (i >= m) {
return lead ^ 1;
}
if (!lead && !limit && f[i][mask] != -1) {
return f[i][mask];
}
int up = limit ? s[i] - '0' : 9;
int ans = 0;
for (int j = 0; j <= up; ++j) {
if (lead && j == 0) {
ans += dfs(i + 1, mask, true, limit && j == up);
} else if (mask >> j & 1 ^ 1) {
ans += dfs(i + 1, mask | (1 << j), false, limit && j == up);
}
}
if (!lead && !limit) {
f[i][mask] = ans;
}
return ans;
};
return n - dfs(0, 0, true, true);
}
};