Skip to content

Commit

Permalink
LeetCode's Pascal's Triangle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Tunzeki committed Jun 28, 2023
1 parent c672f59 commit 8a2925b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions javascript/pascalTriangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Visit https://leetcode.com/problems/pascals-triangle/description/
* for full 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
*
* @param {number} numRows
* @return {number[][]}
*/
var generate = function (numRows) {
if (numRows === 1) {
return [[1]];
}

let pascalTriangle = [[1]]
for (let i = 1; i < numRows; i++) {
// Each element in the triangle is an array
pascalTriangle[i] = [];
lengthOfPrevArray = i; // pascalTriangle[i - 1].length
for (let j = 0; j <= lengthOfPrevArray; j++) {
// The first and last numbers in any row (array) of the triangle are 1s
if (j === 0 || j == lengthOfPrevArray) {
pascalTriangle[i][j] = 1;
} else {
// Other numbers in the row (array) are the sums of the two numbers directly above it
// i.e the sums of the numbers in the previous array at the same position, j, plus
// the previous position, j - 1
pascalTriangle[i][j] =
pascalTriangle[i - 1][j - 1] + pascalTriangle[i - 1][j];
}
}
}

return pascalTriangle;



};

0 comments on commit 8a2925b

Please sign in to comment.