-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPermutationCoefficient.py
50 lines (39 loc) · 1.05 KB
/
PermutationCoefficient.py
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
50
"""
@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
"""
def calculateCoefficientHelper(n, r, lookup):
# Base case
if n < r:
return 0
# If r is zero, or n is equal to r
if r == 0:
return 1
# Get the memoized value
if lookup[n][r] != -1:
return lookup[n][r]
lookup[n][r] = calculateCoefficientHelper(
n - 1, r, lookup) + r * calculateCoefficientHelper(n - 1, r - 1, lookup)
return lookup[n][r]
def calculateCoefficient(n, r):
# Lookup table
lookup = [[-1 for y in range(r + 1)] for x in range(n + 1)]
# Fill the array with default values
return calculateCoefficientHelper(n, r, lookup)
if __name__ == "__main__":
n = 10
r = 2
print(calculateCoefficient(n, r))
n = 10
r = 3
print(calculateCoefficient(n, r))
n = 10
r = 0
print(calculateCoefficient(n, r))
n = 10
r = 1
print(calculateCoefficient(n, r))