-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.3.py
30 lines (20 loc) · 922 Bytes
/
3.3.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
def solution(m):
# initialize memo table T with 0s
T = [[0 for i in range(m + 1)] for j in range(m + 1)]
T[1][1] = 1 # base case
# define T(n, i): number of distinct partitions of n
# such that no term is smaller than i
# Recurrence used,
# T(n, i) = 1 + sum T(n - j, j + 1) over i <= j <= floor {(n - 1)/2}
for n in range(2, m + 1):
for i in range(1, m + 1):
result = 1
lim = int((n - 1)/2)
for j in range(i, lim + 1):
result += T[n - j][j + 1]
T[n][i] = result
if (n, i) == (m, 1): break
# Clearly the solution is T(m, 1), however we substract 1 as the number
# itself is not asked (in the specific question) to be considered as a partition
return(T[m][1] - 1)
print(solution(200))