File tree 2 files changed +70
-0
lines changed
2 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 603
603
790|[ Domino and Tromino Tiling] ( ./0790-domino-and-tromino-tiling.js ) |Medium|
604
604
791|[ Custom Sort String] ( ./0791-custom-sort-string.js ) |Medium|
605
605
792|[ Number of Matching Subsequences] ( ./0792-number-of-matching-subsequences.js ) |Medium|
606
+ 793|[ Preimage Size of Factorial Zeroes Function] ( ./0793-preimage-size-of-factorial-zeroes-function.js ) |Hard|
606
607
796|[ Rotate String] ( ./0796-rotate-string.js ) |Easy|
607
608
802|[ Find Eventual Safe States] ( ./0802-find-eventual-safe-states.js ) |Medium|
608
609
804|[ Unique Morse Code Words] ( ./0804-unique-morse-code-words.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 793. Preimage Size of Factorial Zeroes Function
3
+ * https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/
4
+ * Difficulty: Hard
5
+ *
6
+ * Let f(x) be the number of zeroes at the end of x!. Recall that x! = 1 * 2 * 3 * ... * x
7
+ * and by convention, 0! = 1.
8
+ *
9
+ * For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because
10
+ * 11! = 39916800 has two zeroes at the end.
11
+ *
12
+ * Given an integer k, return the number of non-negative integers x have the property that f(x) = k.
13
+ */
14
+
15
+ /**
16
+ * @param {number } k
17
+ * @return {number }
18
+ */
19
+ var preimageSizeFZF = function ( k ) {
20
+ return findUpperBound ( k ) - findLowerBound ( k ) ;
21
+ } ;
22
+
23
+ function findLowerBound ( k ) {
24
+ let left = 0 ;
25
+ let right = 5 * ( 10 ** 10 ) ;
26
+
27
+ while ( left < right ) {
28
+ const mid = Math . floor ( ( left + right ) / 2 ) ;
29
+ const zeroes = countTrailingZeroes ( mid ) ;
30
+
31
+ if ( zeroes < k ) {
32
+ left = mid + 1 ;
33
+ } else {
34
+ right = mid ;
35
+ }
36
+ }
37
+
38
+ return left ;
39
+ }
40
+
41
+ function findUpperBound ( k ) {
42
+ let left = 0 ;
43
+ let right = 5 * ( 10 ** 10 ) ;
44
+
45
+ while ( left < right ) {
46
+ const mid = Math . floor ( ( left + right ) / 2 ) ;
47
+ const zeroes = countTrailingZeroes ( mid ) ;
48
+
49
+ if ( zeroes <= k ) {
50
+ left = mid + 1 ;
51
+ } else {
52
+ right = mid ;
53
+ }
54
+ }
55
+
56
+ return left ;
57
+ }
58
+
59
+ function countTrailingZeroes ( n ) {
60
+ let count = 0 ;
61
+ let powerOfFive = 5 ;
62
+
63
+ while ( n >= powerOfFive ) {
64
+ count += Math . floor ( n / powerOfFive ) ;
65
+ powerOfFive *= 5 ;
66
+ }
67
+
68
+ return count ;
69
+ }
You can’t perform that action at this time.
0 commit comments