Pascal's Triangle

 Pascal Vibes — Building Triangles Like a Pro

Problem Statement

Given an integer numRows, return the first numRows of Pascal's Triangle.

Each number in the triangle is the sum of the two numbers directly above it.

Example:

Input: numRows = 5
Output:

[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]

Best Data Structure for Solving the Problem

This is a CLASSIC example of Tabulation-based Dynamic Programming 
We're filling a triangle row-by-row where:

triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]

So we’ll use:

  • A 2D List<List<Integer>> (or array of arrays)

  • No need for fancy trees, heaps, or graphs here. Just pure math and memory 

Different Approaches (from Brute Force to Optimized)

Approach 1: Build Row by Row Using Previous Row

This is the “OG” method — intuitive and easy to visualize.

  • Start with [1]

  • For each new row:

    • Add 1 at the start and end

    • In-between values are sum of two values above it

class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> triangle = new ArrayList<>(); for (int i = 0; i < numRows; i++) { List<Integer> row = new ArrayList<>(); row.add(1); for (int j = 1; j < i; j++) { int prevRowVal = triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j); row.add(prevRowVal); } if (i > 0) row.add(1); triangle.add(row); } return triangle; } }

Approach 2: Combinatorics (nCr style)

C(n,r)=n!r!(nr)!C(n, r) = \frac{n!}{r!(n - r)!}

But wait — that’s a factorial mess. So we optimize:

C(n,r+1)=C(n,r)(nr)r+1C(n, r+1) = \frac{C(n, r) \cdot (n - r)}{r + 1}

Less factorials, more speed. 

class Solution { private List<Integer> generateRow(int row) { long ans = 1; List<Integer> ansRow = new ArrayList<>(); ansRow.add(1); for (int col = 1; col < row; col++) { ans = ans * (row - col); ans = ans / col; ansRow.add((int) ans); } return ansRow; } public List<List<Integer>> generate(int numRows) { List<List<Integer>> triangle = new ArrayList<>(); for (int i = 1; i <= numRows; i++) { triangle.add(generateRow(i)); } return triangle; } }

Approach 3: Dynamic Programming (Tabulation)

We build the triangle from top to bottom using previously computed results:

class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> dp = new ArrayList<>(); for (int i = 0; i < numRows; i++) { dp.add(new ArrayList<>()); for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { dp.get(i).add(1); } else { int val = dp.get(i - 1).get(j - 1) + dp.get(i - 1).get(j); dp.get(i).add(val); } } } return dp; } }

⚠️ Common Mistakes and Pitfalls

  • Using n! directly → overflow central

  • Off-by-one errors in loops

  • Forgetting to convert long to int (or not handling big numbers)

  • Assuming you always need the previous row — not with math magic!

📊 Time and Space Complexity Analysis

Approach          Time ComplexitySpace Complexity
Brute Force (Row by Row)            O(n²)                O(n²)
Math (nCr Formula)            O(n²)                O(n²), but less memory 
Dynamic Programming                  O(n²)                                   O(n²)

n = numRows

🌍 Real-World Applications

  • Probability & Combinatorics – calculating outcomes and combinations

  • Binomial Expansions in algebra (ya know that (a + b)^n magic)

  • Pascal’s Triangle Patterns in genetics and fractals

  • Game Dev: Predicting outcomes or generating patterns dynamically


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Divisible Drama — Who’s In, Who’s Out?

Trailing Zeros in Factorial – How Many and Why?

Minimum equal sum