-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path119.py
65 lines (49 loc) · 1.66 KB
/
119.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# [ LeetCode ] 119. Pascal's Triangle II
def solution(rowIndex: int) -> list[int]:
answer: list[int] = [1]
for row in range(1, rowIndex + 1):
stored: int = answer[0]
for index in range(1, row):
stored, answer[index] = answer[index], stored + answer[index]
answer.append(1)
return answer
def another_solution(rowIndex: int) -> list[int]:
answer: list[int] = [1]
for row in range(1, rowIndex + 1):
for index in range(row - 1, 0, -1):
answer[index] = answer[index] + answer[index - 1]
answer.append(1)
return answer
def math_solution(rowIndex: int) -> list[int]:
def combinator(n: int, r: int) -> int:
top, bottom = 1, 1
for number in range(n, n - r, -1):
top *= number
for number in range(r, 0, -1):
bottom *= number
return top // bottom
answer: list[int] = [
combinator(n=rowIndex, r=index)
for index in range(rowIndex)
]
answer.append(1)
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, int] | list[int]]] = [
{
"input": { "rowIndex": 3 },
"output": [1, 3, 3, 1]
},
{
"input": { "rowIndex": 0 },
"output": [1]
},
{
"input": { "rowIndex": 1 },
"output": [1, 1]
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])
assert case["output"] == math_solution(**case["input"])