We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 540e510 commit 2be9ca3Copy full SHA for 2be9ca3
0118-pascals-triangle/solution.py
@@ -1,18 +1,23 @@
1
-# Approach 1 - Dynamic Programming (Iterative)
+# Appraoch 1: Dynamic Programming
2
+
3
+# Time: O(numRows ^ 2)
4
+# Space: O(1)
5
6
class Solution:
7
def generate(self, numRows: int) -> List[List[int]]:
8
triangle = []
-
9
10
for row_num in range(numRows):
11
row = [None for _ in range(row_num + 1)]
12
+ # The first and last row elements are always 1.
13
row[0], row[-1] = 1, 1
14
15
+ # Each triangle element is equal to the sum of the elements
16
+ # above-and-to-the-left and above-and-to-the-right.
17
for j in range(1, len(row) - 1):
18
row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j]
19
20
triangle.append(row)
21
22
return triangle
23
0 commit comments