Description:
Given an integer numRows
, find the first numRows
of Pascal's triangle. In Pascal's triangle, each number is formed by the sum of the two numbers directly above it except for boundaries(with value 1).
Example 1:
Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1 Output: [[1]]
Algorithmic Steps This problem is solved with the help of sequential numbers sum and average calculations. The algorithmic approach can be summarized as follows:
-
Initialize the triangle(
triangle
) with an empty to hold the rows of Pascal triangle. -
Iterate over the rows starting from
0
to the given number of rows(numRows
) provided. -
Create each row initialized with
row+1
array elements, all set to 1. -
Iterate over the columns starting from
1
torow-1
to calculate the new row values. Each cell value is calculated by the sum of two elements directly above it from the previous row. -
Add the completed row to the triangle list.
-
Return the completed triangle upon generation of each row completed.
Time and Space complexity:
This algorithm has a time complexity of O(n^2)
, where n
is the number of given rows. This is because we need to iterate over n
rows and for each row, there can be upto n
valued to be generated.
The output array needs O(n^2)
space to accomodate all the triangle elements, where n
is the number of given rows.. Hence, the total space complexity is O(n^2)
.