forked from everthis/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1012-numbers-with-repeated-digits.js
96 lines (85 loc) · 1.91 KB
/
1012-numbers-with-repeated-digits.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @param {number} n
* @return {number}
*/
function numDupDigitsAtMostN(n) {
let numNoDupDigits = 0; // the number of positive integers less than or equal to n with no repeated digits
let lst = Array.from(String(n), Number);
let n_digits = lst.length;
// if n = 8765, lst = [8,7,6,5],
// the number without repeated digit can the the following format:
// XXX
// XX
// X
for (let i = 1; i < n_digits; i++) {
// the number of i digits without repeated digit
// the leading digit cannot be 0
numNoDupDigits += 9 * perm(9, i - 1);
}
// and
// 1XXX ~ 7XXX
// 80XX ~ 86XX
// 870X ~ 875X
// 8760 ~ 8764
let seen = new Set();
for (let i = 0; i < lst.length; i++) {
let x = lst[i];
for (let y = (i === 0 ? 1 : 0); y < x; y++) {
if (!seen.has(y)) {
// the leading digit used - y
// for the remaining positions we cannot use digits in set seen and y
numNoDupDigits += perm(9 - i, n_digits - i - 1);
}
}
if (seen.has(x)) {
break;
}
seen.add(x);
}
// and
// 8765
if (n_digits === new Set(lst).size) {
numNoDupDigits += 1;
}
return n - numNoDupDigits;
}
function perm(m, n) {
let res = 1
for(let i = 0; i < n; i++) {
res *= m
m--
}
return res
}
// another
/**
* @param {number} n
* @return {number}
*/
var numDupDigitsAtMostN = function(n) {
const digits = [], {floor} = Math
let tmp = n + 1
while(tmp) {
digits.push(tmp % 10)
tmp = floor(tmp / 10)
}
let res = 0
const len = digits.length
let cur = 9
for(let i = 0; i < len - 1; i++) {
res += cur
cur *= (9 - i)
}
cur = floor(cur / 9)
const seen = Array(10).fill(false)
for(let i = 0; i < len; i++) {
const d = digits[len - i - 1]
for(let j = (i === 0 ? 1 : 0); j < d; j++) {
if(!seen[j]) res += cur
}
cur = floor(cur / (9 - i))
if(seen[d]) break
seen[d] = true
}
return n - res
};