Skip to content

Commit 71ab07f

Browse files
committed
LC 119. Pascal's Triangle II (Python DP)
1 parent 4a73d30 commit 71ab07f

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
113113
| [114. Flatten Binary Tree to Linked List][lc114] | 🟠 Medium | [![python](res/py.png)][lc114py] |
114114
| [115. Distinct Subsequences][lc115] | 🔴 Hard | [![python](res/py.png)][lc115py] |
115115
| [118. Pascal's Triangle][lc118] | 🟢 Easy | [![python](res/py.png)][lc118py] [![rust](res/rs.png)][lc118rs] |
116-
| [119. Pascal's Triangle II][lc119] | 🟢 Easy | [![rust](res/rs.png)][lc119rs] |
116+
| [119. Pascal's Triangle II][lc119] | 🟢 Easy | [![python](res/py.png)][lc119py] [![rust](res/rs.png)][lc119rs] |
117117
| [120. Triangle][lc120] | 🟠 Medium | [![python](res/py.png)][lc120py] |
118118
| [121. Best Time to Buy and Sell Stock][lc121] | 🟢 Easy | [![python](res/py.png)][lc121py] [![rust](res/rs.png)][lc121rs] |
119119
| [124. Binary Tree Maximum Path Sum][lc124] | 🔴 Hard | [![python](res/py.png)][lc124py] |
@@ -827,6 +827,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
827827
[lc118py]: leetcode/pascals-triangle.py
828828
[lc118rs]: leetcode/pascals-triangle.rs
829829
[lc119]: https://leetcode.com/problems/pascals-triangle-ii/
830+
[lc119py]: leetcode/pascals-triangle-ii.py
830831
[lc119rs]: leetcode/pascals-triangle-ii.rs
831832
[lc120]: https://leetcode.com/problems/triangle/
832833
[lc120py]: leetcode/triangle.py

leetcode/pascals-triangle-ii.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 119. Pascal's Triangle II
2+
# 🟢 Easy
3+
#
4+
# https://leetcode.com/problems/pascals-triangle-ii/
5+
#
6+
# Tags: Array - Dynamic Programming
7+
8+
import timeit
9+
from typing import List
10+
11+
12+
# Iterate over n number of rows, for each row, iterate over the row
13+
# elements backwards computing their new value as the sum of the element
14+
# at the same position plus the one at the previous position on the
15+
# previous row.
16+
#
17+
# Time complexity: O(n^2) - We iterate n rows, for each row, we iterate
18+
# over all the row elements.
19+
# Space complexity: O(n) - We use an extra array of size n.
20+
#
21+
# Runtime 35 ms Beats 77.91%
22+
# Memory 16.04 MB Beats 96.64%
23+
class Solution:
24+
def getRow(self, rowIndex: int) -> List[int]:
25+
row = [0] * (rowIndex + 1)
26+
row[0] = 1
27+
for i in range(1, rowIndex + 1):
28+
for j in range(i, 0, -1):
29+
row[j] += row[j - 1]
30+
return row
31+
32+
33+
def test():
34+
executors = [Solution]
35+
tests = [
36+
[0, [1]],
37+
[1, [1, 1]],
38+
[3, [1, 3, 3, 1]],
39+
]
40+
for executor in executors:
41+
start = timeit.default_timer()
42+
for _ in range(1):
43+
for col, t in enumerate(tests):
44+
sol = executor()
45+
result = sol.getRow(t[0])
46+
exp = t[1]
47+
assert result == exp, (
48+
f"\033[93m» {result} <> {exp}\033[91m for"
49+
+ f" test {col} using \033[1m{executor.__name__}"
50+
)
51+
stop = timeit.default_timer()
52+
used = str(round(stop - start, 5))
53+
cols = "{0:20}{1:10}{2:10}"
54+
res = cols.format(executor.__name__, used, "seconds")
55+
print(f"\033[92m» {res}\033[0m")
56+
57+
58+
test()

0 commit comments

Comments
 (0)