Skip to content

Commit 890aeb9

Browse files
authored
Update 2376-count-special-integers.js
1 parent cc45ae0 commit 890aeb9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2376-count-special-integers.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,36 @@ var countSpecialNumbers = function(n) {
2525
function A(m, n) {
2626
return n === 0 ? 1 : A(m, n - 1) * (m - n + 1);
2727
}
28+
29+
// another
30+
31+
/**
32+
* @param {number} n
33+
* @return {number}
34+
*/
35+
const countSpecialNumbers = function (n) {
36+
const s = '' + n
37+
const dp = Array.from({ length: 11 }, () =>
38+
Array.from({ length: 2 }, () => Array(1024).fill(-1))
39+
)
40+
41+
return helper(0, 1, 0, s)
42+
function helper(idx, tight = 1, mask = 0, digits) {
43+
if (idx == digits.length) return mask !== 0 ? 1 : 0
44+
45+
if (dp[idx][tight][mask] != -1) return dp[idx][tight][mask]
46+
47+
let k = tight ? +digits[idx] : 9
48+
let ans = 0
49+
50+
for (let i = 0; i <= k; i++) {
51+
if (mask & (1 << i)) continue
52+
let newMask = mask == 0 && i == 0 ? mask : mask | (1 << i)
53+
54+
let nextTight = tight && i == digits[idx] ? 1 : 0
55+
ans += helper(idx + 1, nextTight, newMask, digits)
56+
}
57+
58+
return (dp[idx][tight][mask] = ans)
59+
}
60+
}

0 commit comments

Comments
 (0)