Skip to content

Commit 59e4782

Browse files
committed
Add solution #1006
1 parent 29aedc7 commit 59e4782

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

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

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

@@ -814,6 +814,7 @@
814814
1003|[Check If Word Is Valid After Substitutions](./solutions/1003-check-if-word-is-valid-after-substitutions.js)|Medium|
815815
1004|[Max Consecutive Ones III](./solutions/1004-max-consecutive-ones-iii.js)|Medium|
816816
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|
817818
1009|[Complement of Base 10 Integer](./solutions/1009-complement-of-base-10-integer.js)|Easy|
818819
1010|[Pairs of Songs With Total Durations Divisible by 60](./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
819820
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|

solutions/1006-clumsy-factorial.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
};

0 commit comments

Comments
 (0)