Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
[ , [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
If we have the a row of Pascal\'s triangle, we can easily compute the next\nrow by each pair of adjacent values.\n
Although the algorithm is very simple, the iterative approach to constructing\nPascal\'s triangle can be classified as dynamic programming because we\nconstruct each row based on the previous row.\n
First, we generate the overall
triangle list, which will store each row as\na sublist. Then, we check for the special case of , as we would otherwise\nreturn . If , then we initialize
triangle with \nas its first row, and proceed to fill the rows as follows:
Time complexity : \n\n
Although updating each value of
triangle happens in constant time, it\nis performed times. To see why, consider how many\noverall loop iterations there are. The outer loop obviously runs\n times, but for each iteration of the outer loop, the inner\nloop runs times. Therefore, the overall number of
triangle updates\nthat occur is , which, according to Gauss\' formula,\nis
Space complexity : \n\n
Because we need to store each number that we update in
triangle, the\nspace requirement is the same as the time complexity.
Analysis and solutions written by: @emptyset\n