Skip to content

Commit dc07b82

Browse files
authored
Create 2376-count-special-integers.js
1 parent f8a0930 commit dc07b82

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2376-count-special-integers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var countSpecialNumbers = function(n) {
6+
const L = [];
7+
for (let x = n + 1; x > 0; x = Math.floor(x / 10)) L.unshift(x % 10);
8+
9+
// Count the number with digits < N
10+
let res = 0,
11+
limit = L.length;
12+
for (let i = 1; i < limit; ++i) res += 9 * A(9, i - 1);
13+
14+
const seen = new Set();
15+
for (let i = 0; i < limit; ++i) {
16+
for (let j = i > 0 ? 0 : 1; j < L[i]; ++j)
17+
if (!seen.has(j)) res += A(9 - i, limit - i - 1);
18+
if (seen.has(L[i])) break;
19+
seen.add(L[i]);
20+
}
21+
return res;
22+
};
23+
24+
25+
function A(m, n) {
26+
return n === 0 ? 1 : A(m, n - 1) * (m - n + 1);
27+
}

0 commit comments

Comments
 (0)