Skip to content

Commit 95401de

Browse files
authored
Update 902-numbers-at-most-n-given-digit-set.js
1 parent e06f560 commit 95401de

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

902-numbers-at-most-n-given-digit-set.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
/**
2+
* @param {string[]} digits
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
const atMostNGivenDigitSet = function(digits, n) {
7+
let res = 0
8+
const str = `${n}`, len = str.length
9+
const { pow } = Math, base = digits.length
10+
for(let i = 1; i < len; i++) {
11+
res += pow(base, i)
12+
}
13+
14+
dfs(0)
15+
16+
return res
17+
18+
function dfs(pos) {
19+
if(pos === len) {
20+
res++
21+
return
22+
}
23+
for(const ch of digits) {
24+
if(str[pos] > ch) {
25+
res += pow(base, len - 1 - pos)
26+
} else if(str[pos] === ch) {
27+
dfs(pos + 1)
28+
}
29+
}
30+
}
31+
};
32+
33+
// another
34+
35+
136
/**
237
* @param {string[]} digits
338
* @param {number} n

0 commit comments

Comments
 (0)