File tree 2 files changed +50
-1
lines changed
2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,086 LeetCode solutions in JavaScript
1
+ # 1,087 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
820
820
1009|[ Complement of Base 10 Integer] ( ./solutions/1009-complement-of-base-10-integer.js ) |Easy|
821
821
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
822
822
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|
823
824
1022|[ Sum of Root To Leaf Binary Numbers] ( ./solutions/1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
824
825
1023|[ Camelcase Matching] ( ./solutions/1023-camelcase-matching.js ) |Medium|
825
826
1028|[ Recover a Tree From Preorder Traversal] ( ./solutions/1028-recover-a-tree-from-preorder-traversal.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments