AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
Here is the full implementation.
/**
* @param {number} numRows
* @return {number[][]}
*/
let generate = function(numRows) {
let triangle = [];
if (numRows === 0) {
return triangle;
}
triangle.push([1]);
for (let i = 1; i < numRows; i++) {
let prevRow = triangle[i - 1];
let newRow = [];
newRow.push(1);
for (let j = 1; j < prevRow.length; j++) {
newRow.push(prevRow[(j - 1)] + prevRow[j]);
}
newRow.push(1);
triangle.push(newRow);
}
return triangle;
};