File tree 2 files changed +47
-1
lines changed
2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,082 LeetCode solutions in JavaScript
1
+ # 1,083 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
814
814
1003|[ Check If Word Is Valid After Substitutions] ( ./solutions/1003-check-if-word-is-valid-after-substitutions.js ) |Medium|
815
815
1004|[ Max Consecutive Ones III] ( ./solutions/1004-max-consecutive-ones-iii.js ) |Medium|
816
816
1005|[ Maximize Sum Of Array After K Negations] ( ./solutions/1005-maximize-sum-of-array-after-k-negations.js ) |Easy|
817
+ 1006|[ Clumsy Factorial] ( ./solutions/1006-clumsy-factorial.js ) |Medium|
817
818
1009|[ Complement of Base 10 Integer] ( ./solutions/1009-complement-of-base-10-integer.js ) |Easy|
818
819
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
819
820
1022|[ Sum of Root To Leaf Binary Numbers] ( ./solutions/1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1006. Clumsy Factorial
3
+ * https://leetcode.com/problems/clumsy-factorial/
4
+ * Difficulty: Medium
5
+ *
6
+ * The factorial of a positive integer n is the product of all positive integers less
7
+ * than or equal to n.
8
+ * - For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
9
+ *
10
+ * We make a clumsy factorial using the integers in decreasing order by swapping out the
11
+ * multiply operations for a fixed rotation of operations with multiply '*', divide '/',
12
+ * add '+', and subtract '-' in this order.
13
+ * - For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.
14
+ *
15
+ * However, these operations are still applied using the usual order of operations of
16
+ * arithmetic. We do all multiplication and division steps before any addition or
17
+ * subtraction steps, and multiplication and division steps are processed left to right.
18
+ *
19
+ * Additionally, the division that we use is floor division such that 10 * 9 / 8 = 90 / 8 = 11.
20
+ *
21
+ * Given an integer n, return the clumsy factorial of n.
22
+ */
23
+
24
+ /**
25
+ * @param {number } n
26
+ * @return {number }
27
+ */
28
+ var clumsy = function ( n ) {
29
+ if ( n <= 2 ) return n ;
30
+ if ( n === 3 ) return 6 ;
31
+
32
+ let result = Math . floor ( n * ( n - 1 ) / ( n - 2 ) ) + ( n - 3 ) ;
33
+ n -= 4 ;
34
+
35
+ while ( n >= 4 ) {
36
+ result -= Math . floor ( n * ( n - 1 ) / ( n - 2 ) ) - ( n - 3 ) ;
37
+ n -= 4 ;
38
+ }
39
+
40
+ if ( n === 3 ) result -= 6 ;
41
+ else if ( n === 2 ) result -= 2 ;
42
+ else if ( n === 1 ) result -= 1 ;
43
+
44
+ return result ;
45
+ } ;
You can’t perform that action at this time.
0 commit comments