-
Notifications
You must be signed in to change notification settings - Fork 0
/
295.py
51 lines (39 loc) · 1012 Bytes
/
295.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Problem:
Pascal's triangle is a triangular array of integers constructed with the following
formula:
The first row consists of the number 1. For each subsequent row, each element is the
sum of the numbers directly above it, on either side. For example, here are the first
few rows:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Given an input k, return the kth row of Pascal's triangle.
Bonus: Can you do this using only O(k) space?
"""
from typing import List
def get_pascal(k: int) -> List[int]:
row = [1 for _ in range(k)]
curr = 1
for _ in range(k):
# generating the value for each level
last = 0
for i in range(curr - 1):
last, temp = row[i], last
row[i] += temp
curr += 1
return row
if __name__ == "__main__":
print(get_pascal(1))
print(get_pascal(2))
print(get_pascal(3))
print(get_pascal(4))
print(get_pascal(5))
print(get_pascal(6))
"""
SPECS:
TIME COMPLEXITY: O(k ^ 2)
SPACE COMPLEXITY: O(k)
"""