-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
46 lines (38 loc) · 1.04 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const cache = new Map();
const factorial = (n) => {
let sum = 1n;
if (cache.has(n)) return cache.get(n);
let tmpN = n;
while (n > 0n) {
sum *= n;
n--;
}
cache.set(tmpN, sum);
return sum;
}
const getCombination = (n, r) => {
n = BigInt(n), r = BigInt(r);
return factorial(n) / (factorial(r) * factorial(n - r));
}
/**
* @param {number[]} nums
* @return {number}
*/
const numOfWays = (nums) => {
const findPossibleWays = (numsList) => {
if (numsList.length <= 2) return 1n;
let root = numsList.shift(), leftList = [], rightList = [], leftCount = 0, rightCount = 0, total = 0;
while (numsList.length > 0) {
total++;
let curr = numsList.shift();
if (curr < root) {
leftList.push(curr);
} else {
rightList.push(curr);
}
}
leftCount = leftList.length, rightCount = rightList.length;
return findPossibleWays(leftList) * findPossibleWays(rightList) * getCombination(total, leftCount);
}
return (findPossibleWays(nums) - 1n) % BigInt(Math.pow(10, 9) + 7);
};