-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPermutationCoefficient.js
49 lines (43 loc) · 1.04 KB
/
PermutationCoefficient.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
47
48
49
/**
* @author Anirudh Sharma
*
* Given two integers n and r, find nPr.
* Since the answer may be very large, calculate the answer modulo 10^9+7.
*
* Constraints:
* 1 ≤ n ≤ 1000
* 1 ≤ r ≤ 800
*/
const calculateCoefficient = (n, r) => {
// Lookup table
const lookup = Array.from(Array(n + 1), () => Array(r + 1).fill(-1));
// Fill the array with default values
return calculateCoefficientHelper(n, r, lookup);
};
const calculateCoefficientHelper = (n, r, lookup) => {
if (n < r) {
return 0;
}
if (r === 0) {
return 1;
}
if (lookup[n][r] !== -1) {
return lookup[n][r];
}
return lookup[n][r] = calculateCoefficient(n - 1, r) + r * calculateCoefficient(n - 1, r - 1);
};
const main = () => {
let n = 10;
let r = 2;
console.log(calculateCoefficient(n, r));
n = 10;
r = 3;
console.log(calculateCoefficient(n, r));
n = 10;
r = 0;
console.log(calculateCoefficient(n, r));
n = 10;
r = 1;
console.log(calculateCoefficient(n, r));
};
main();