Pascals Triangle
- Shreyas Naphad
- Jul 24, 2024
- 1 min read
Problem Statement: Given an integer numRows, generate the first numRows of Pascal's Triangle. In Pascal's Triangle, each number is the sum of the two numbers directly above it.
Solution:
To generate Pascal's Triangle, we start with the first row containing a single 1. Each subsequent row is generated by adding the two numbers directly above each element, with 1 at the start and end of each row.
Steps to Solve:
1. Initialize the result list with the first row [1].
2. Iterate from 1 to numRows-1:
Create a new row starting with 1.
For each element in the current row (excluding the first and last elements), calculate the sum of the two numbers directly above it.
End the row with 1 and add it to the result list.
Time Complexity: O(numRows^2)
Space Complexity: O(numRows^2)
Comments