We can generate the current row using the previous row. A row
getRow(n):- Base case: If
n == 0, return[1].
- Base case: If
- Recursive step:
prev_row = getRow(n - 1). - Create
curr_rowstarting and ending with 1. - Intermediate elements:
curr_row[i] = prev_row[i-1] + prev_row[i]. - Return
curr_row.
- Time Complexity: O(N^2).
- Space Complexity: O(N).
def get_row(rowIndex):
if rowIndex == 0:
return [1]
prev_row = get_row(rowIndex - 1)
res = [1]
for i in range(1, len(prev_row)):
res.append(prev_row[i-1] + prev_row[i])
res.append(1)
return res