Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fd19168

Browse files
committedMar 31, 2025
Add solution #1012
1 parent ce76a0d commit fd19168

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
 

Diff for: ‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,086 LeetCode solutions in JavaScript
1+
# 1,087 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -820,6 +820,7 @@
820820
1009|[Complement of Base 10 Integer](./solutions/1009-complement-of-base-10-integer.js)|Easy|
821821
1010|[Pairs of Songs With Total Durations Divisible by 60](./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
822822
1011|[Capacity To Ship Packages Within D Days](./solutions/1011-capacity-to-ship-packages-within-d-days.js)|Medium|
823+
1012|[Numbers With Repeated Digits](./solutions/1012-numbers-with-repeated-digits.js)|Hard|
823824
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
824825
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
825826
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|

Diff for: ‎solutions/1012-numbers-with-repeated-digits.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 1012. Numbers With Repeated Digits
3+
* https://leetcode.com/problems/numbers-with-repeated-digits/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer n, return the number of positive integers in the range [1, n] that have at
7+
* least one repeated digit.
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var numDupDigitsAtMostN = function(n) {
15+
const digits = String(n).split('').map(Number);
16+
const length = digits.length;
17+
let result = 0;
18+
19+
function calculatePermutations(size, available) {
20+
let total = 1;
21+
for (let i = 0; i < size; i++) {
22+
total *= available - i;
23+
}
24+
return total;
25+
}
26+
27+
for (let i = 1; i < length; i++) {
28+
result += 9 * calculatePermutations(i - 1, 9);
29+
}
30+
31+
const used = new Set();
32+
for (let i = 0; i < length; i++) {
33+
const start = i === 0 ? 1 : 0;
34+
const limit = digits[i];
35+
36+
for (let d = start; d < limit; d++) {
37+
if (!used.has(d)) {
38+
result += calculatePermutations(length - i - 1, 10 - used.size - 1);
39+
}
40+
}
41+
42+
if (used.has(digits[i])) break;
43+
used.add(digits[i]);
44+
if (i === length - 1) result++;
45+
}
46+
47+
return n - result;
48+
};

0 commit comments

Comments
 (0)
Please sign in to comment.