|
| 1 | +# Pascal's Triangle - Problem #118 |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | +Given an integer `numRows`, return the first `numRows` of Pascal's triangle. |
| 5 | + |
| 6 | +In Pascal's triangle, each number is the sum of the two numbers directly above it as follows: |
| 7 | + |
| 8 | +## Examples |
| 9 | +``` |
| 10 | +Input: numRows = 5 |
| 11 | +Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] |
| 12 | +
|
| 13 | +Input: numRows = 1 |
| 14 | +Output: [[1]] |
| 15 | +``` |
| 16 | + |
| 17 | +## Approach |
| 18 | +**Key Insight**: Each row can be generated based on the previous row. The edges are always 1, and inner elements are the sum of the two elements from the previous row. |
| 19 | + |
| 20 | +**Algorithm**: |
| 21 | +1. Initialize a list of lists to store the triangle. |
| 22 | +2. For each row from 0 to numRows-1: |
| 23 | + - Create a new list for the current row. |
| 24 | + - Set the first and last elements to 1. |
| 25 | + - For inner positions, set value to sum of previous row's adjacent elements. |
| 26 | +3. Return the triangle. |
| 27 | + |
| 28 | +**Why this works**: |
| 29 | +- Builds the triangle row by row, using the definition of Pascal's triangle. |
| 30 | +- Efficiently computes each element without redundant calculations. |
| 31 | + |
| 32 | +## Complexity Analysis |
| 33 | +- **Time Complexity**: O(numRows²) - We iterate through each row and each element in the row. |
| 34 | +- **Space Complexity**: O(numRows²) - To store the entire triangle. |
| 35 | + |
| 36 | +## Key Insights |
| 37 | +- Pascal's triangle rows are binomial coefficients. |
| 38 | +- Can be optimized to O(numRows) space by generating in-place or only keeping previous row. |
| 39 | +- Useful in combinatorics and probability. |
| 40 | + |
| 41 | +## Alternative Approaches |
| 42 | +1. **Mathematical Formula**: Use binomial coefficients C(n, k) = n! / (k!(n-k)!) for each position - but less efficient due to factorial computations. |
| 43 | +2. **Single Row Generation**: Generate each row independently without storing previous ones, but still O(n²) time. |
| 44 | + |
| 45 | +## Solutions in Different Languages |
| 46 | + |
| 47 | +### Java |
| 48 | +```java |
| 49 | +// See solution.java |
| 50 | +import java.util.*; |
| 51 | + |
| 52 | +class Solution { |
| 53 | + public List<List<Integer>> generate(int numRows) { |
| 54 | + List<List<Integer>> triangle = new ArrayList<>(); |
| 55 | + |
| 56 | + for (int i = 0; i < numRows; i++) { |
| 57 | + List<Integer> row = new ArrayList<>(); |
| 58 | + for (int j = 0; j <= i; j++) { |
| 59 | + if (j == 0 || j == i) { |
| 60 | + row.add(1); |
| 61 | + } else { |
| 62 | + row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j)); |
| 63 | + } |
| 64 | + } |
| 65 | + triangle.add(row); |
| 66 | + } |
| 67 | + |
| 68 | + return triangle; |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### JavaScript |
| 74 | +```javascript |
| 75 | +// See solution.js |
| 76 | +/** |
| 77 | + * @param {number} numRows |
| 78 | + * @return {number[][]} |
| 79 | + */ |
| 80 | +var generate = function(numRows) { |
| 81 | + const triangle = []; |
| 82 | + |
| 83 | + for (let i = 0; i < numRows; i++) { |
| 84 | + const row = []; |
| 85 | + for (let j = 0; j <= i; j++) { |
| 86 | + if (j === 0 || j === i) { |
| 87 | + row.push(1); |
| 88 | + } else { |
| 89 | + row.push(triangle[i - 1][j - 1] + triangle[i - 1][j]); |
| 90 | + } |
| 91 | + } |
| 92 | + triangle.push(row); |
| 93 | + } |
| 94 | + |
| 95 | + return triangle; |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +## Test Cases |
| 100 | +``` |
| 101 | +Test Case 1: 5 → [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] |
| 102 | +Test Case 2: 1 → [[1]] |
| 103 | +Test Case 3: 0 → [] |
| 104 | +Test Case 4: 3 → [[1],[1,1],[1,2,1]] |
| 105 | +``` |
| 106 | + |
| 107 | +## Edge Cases |
| 108 | +- numRows = 0 (empty triangle) |
| 109 | +- numRows = 1 (single row) |
| 110 | +- Large numRows (up to 30 as per constraints, to avoid integer overflow) |
| 111 | + |
| 112 | +## Related Problems |
| 113 | +- Pascal's Triangle II (generate single row) |
| 114 | +- Binomial Coefficient problems |
| 115 | +- Triangle path problems |
0 commit comments