119. Pascal’s Triangle I & II

class Solution:
def generate(self, numRows: int) -> List[List[int]]:
pascal=[[]]*numRows
for i in range(numRows):
pascal[i]=[0]*(i+1)
pascal[i][0],pascal[i][i]=1,1
for j in range(1,i):
pascal[i][j]=pascal[i-1][j-1]+pascal[i-1][j]
return(pascal)
class Solution:

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

pascal=[[]]*(rowIndex+1)
for i in range(rowIndex+1):
pascal[i]=[0]*(i+1)
pascal[i][0],pascal[i][i]=1,1
for j in range(1,i):
pascal[i][j]=pascal[i-1][j-1]+pascal[i-1][j]
return(pascal[-1])
class Solution:
def combinatorial(self,n,r):
res=1
for i in range(0,r): #4c2= 4*3/1*2
res*=(n-i)
res//=(i+1)
return res
def getRow(self, rowIndex: int) -> List[int]:
result=[]
for i in range(rowIndex+1):
result.append(self.combinatorial(rowIndex,i))
return result
#Time O(rowIndex^2)
#Space O(rowIndex)
class Solution:

def getRow(self, rowIndex: int) -> List[int]:
res=[1]
for i in range(1,rowIndex+1):
res.append(1)
for j in range(len(res)-2,0,-1):
res[j]=res[j-1]+res[j]
return(res)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.