Pascal’s Triangle II

Shubham Singh
1 min readMar 22, 2020

Given a non-negative index k where k ≤ 33, return the kth index row of Pascal’s triangle.

Note that the row index starts from 0.

How can we solve this problem using Recursion? What is the Intuition behind this problem ? is there any other solution?
Let’s see the recursive code first :

class Solution:
def getRow(self, n: int) -> List[int]:
if n == 0:
return [1]
if n == 1:
return [1,1]

prevRow = self.getRow(n-1)
currentRow = [prevRow[i] + prevRow[i+1] for i in \ range(len(prevRow) -1)]
currentRow.insert(0,1)
currentRow.append(1)
print(currentRow)
return currentRow

Alternate Solution:

class Solution:
def getRow(self, rowIndex: int) -> List[int]:

pascal = []
rowIndex+=1
C = 1
for i in range(1, rowIndex + 1):
pascal.append(C)
C = int(C * (rowIndex - i) / i);
return pascal

Thank You and Happy Coding!

--

--

Shubham Singh

I am Software Engineer with 2 years experience working for Dassault Systems and FIS, with a keen interest in Kubernetes and Container Technology.