def pascal(row, col):
if col == 1 or col == row:
return 1
else:
return pascal(row - 1, col) + pascal(row - 1, col - 1)
The above recursive implementation is exponential in terms of time complexity. With the knowledge of Dynamic Programming, how do we write a function fast_pascal(row, col)? The function should take in an integer row and an integer col and return the value in (row, col). Note: row and col starts from 1.
This is what i tried,
def fast_pascal(row,col):
dynamic = [[1]+[1]*(col-1)for i in range(row)]
for row_ind in range(1,row):
for col_ind in range(1,col):
dynamic[row_ind][col_ind] = dynamic[row_ind-1][col_ind] + dynamic[row_ind-1][col_ind-1]
return dynamic[row-1][col-1]
The code gives the wrong results. Its supposed to be,
fast_pascal(3,2) == 2
fast_pascal(4,3) == 3
fast_pascal(500,3) == 124251