-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathProblem-2.py
More file actions
32 lines (27 loc) · 1.07 KB
/
Problem-2.py
File metadata and controls
32 lines (27 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Time Complexity : O(n^2)
# Space Complexity :O(n^2)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :no
# Your code here along with comments explaining your approach
# Start with the first two base rows of Pascal’s Triangle: [1] and [1, 1].
# For each new row, compute inner values by adding two adjacent numbers from the previous row.
# Append 1 at the start and end of each row to complete it, and repeat until all rows are built.
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 1:
return [[1]]
if numRows == 2:
return [[1],[1,1]]
output = [[1],[1,1]]
def res_row(prev,i):
result = []
result.append(1)
for k in range(1,i):
result.append(prev[k-1]+prev[k])
result.append(1)
return result
for i in range(2,numRows):
prev= output[-1]
numrow_result = res_row(prev,i)
output.append(numrow_result)
return output