24. Pascal Triangle
Last updated
Last updated
The problem can be found at the following link: Question Link
To generate the nth row of Pascal's Triangle, I optimize the simple brute force approach as follows:
I initialize two vectors, out
and prev
, both of size n, with all elements set to 1.
I iterate through each row, updating the elements of out
based on the sum of the two corresponding elements from the prev
vector.
I also use modular arithmetic to prevent integer overflow.
In each iteration, I update the out
array with the values from the prev
array.
For example, let's generate the 5th row of Pascal's Triangle:
Time Complexity: O(n^2)
, where n
is the number of rows.
Auxiliary Space Complexity: O(n)
, as I use two vectors of size n
.
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.