Algorithm Day83 - Pascal's Triangle
🧩 Problem Description
Given an integer numRows, return the first numRows of Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
1 | 1 |
💬 Examples
Example 1
1 | Input: numRows = 5 |
Example 2
1 | Input: numRows = 1 |
💡 Intuition
Each row starts and ends with 1. For intermediate elements, the value equals the sum of the two elements above it from the previous row. We can build rows iteratively from top to bottom.
🔢 Java Code (Iterative)
1 | import java.util.*; |
⏱ Complexity Analysis
- Time: O(numRows^2) — we fill each entry of the triangle once.
- Space: O(numRows^2) — output size.
✍️ Summary
- Build each row using the previous row’s values.
- Simple, iterative approach works efficiently for the problem constraints.
Related problems
lc-119— Pascal’s Triangle II (specific row)lc-118variations involving combinatorics