Pascal's Triangle

Arrays FAQs(Medium) Medium

Given an integer numRows return the first numRows rows of Pascal's triangle.


In Pascal's triangle:

  • The first row has one element with a value of 1.
  • Each row has one more element in it than its previous row.
  • The value of each element is equal to the sum of the elements directly above it when arranged in a triangle format.

Examples:

Input: numRows = 4

Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]

Explanation: 1st Row has its value set to 1.

All other cells take their value as the sum of the values directly above them

Input: numRows = 5

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

Explanation: 1st Row has its value set to 1.

All other cells take their value as the sum of the values directly above them

Input: numRows = 3

Constraints

  • 1 <= numRows <= 30

Hints

  • Each element in a row (except the first and last) is the sum of the two elements directly above it from the previous row. This recursive relationship can be used to compute Pascal's triangle row by row.
  • For each row, initialize the first and last elements as 1. Compute the middle elements using values from the previous row.

Company Tags

Cloudflare Dropbox Deloitte Docker Instacart Wayfair Swiggy Stripe Splunk Salesforce NVIDIA Zomato Teladoc Health Unity Technologies Nutanix Micron Technology Freshworks Activision Blizzard Philips Healthcare Flipkart Zoho Alibaba HCL Technologies Bungie Lyft TCS Cognizant Accenture Infosys Capgemini Wipro