Skip to content

Commit 1e7cea6

Browse files
committed
Adding Efficient Solution - Problem - 118 - Pacalas Triangle
1 parent 36d838e commit 1e7cea6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 118
6+
## Problem Name: Pascal's Triangle
7+
##===================================
8+
#
9+
#Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
10+
#
11+
#In Pascal's triangle, each number is the sum of the two numbers directly above it.
12+
#
13+
#Example:
14+
#
15+
#Input: 5
16+
#Output:
17+
#[
18+
# [1],
19+
# [1,1],
20+
# [1,2,1],
21+
# [1,3,3,1],
22+
# [1,4,6,4,1]
23+
#]
24+
class Solution:
25+
def generate(self, numRows):
26+
if numRows is None: #Condition-check: If numRows is empty
27+
return [] #Return empty list
28+
tmp = [] #Initialize tmp list
29+
for i in range(numRows): #Loop through numRows
30+
tmp.append([0]*(i+1)) #Append the list i + 1 in tmp
31+
for i in range(numRows): #Loop through numRows
32+
for j in range(i+1): #Loop through i + 1
33+
if j == 0 or j == i: #Condition-check: If j is zero or j is equal to i
34+
tmp[i][j] = 1 #Update the tmp i row, j column by 1
35+
else: #Condition-check: Else
36+
tmp[i][j] = tmp[i-1][j-1] + tmp[i-1][j] #Update tmp i row, j column by the equation
37+
return tmp #Return tmp

0 commit comments

Comments
 (0)