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
|
int dp[11][1050][2][2][2];
class Solution {
public:
string s;
int dfs(int i, int mask, int lim, int lz, bool ok) {
if(i == s.size()) return ok;
int &v = dp[i][mask][lim][lz][ok];
if(~v) return v;
v = 0;
int up = lim ? s[i] - '0' : 9;
for(int j = 0; j <= up; j += 1) {
if(lz) {
v += dfs(i + 1, j ? (mask | 1 << j) : 0, lim && j == up, lz && j == 0, ok);
} else {
v += dfs(i + 1, mask | 1 << j, lim && j == up, lz, ok || (mask >> j & 1));
}
}
return v;
}
int numDupDigitsAtMostN(int n) {
s = to_string(n);
memset(dp, -1, sizeof dp);
return dfs(0, 0, 1, 1, 0);
}
};
|