Skip to content

Commit

Permalink
Update 0118-pascals-triangle.js
Browse files Browse the repository at this point in the history
  • Loading branch information
enourmo committed Sep 7, 2023
1 parent db7509f commit b695fdf
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions javascript/0118-pascals-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@
// the time complexity will basically be the number of elements in pascale tringle. roughly height of tringle * number of honeycomb in each row.
// O(n^2);

var generate = function(num) {

const outerArray = [];
// adding first two rows of pascals triangle
if (num >= 2) {
outerArray.push([1]);
outerArray.push([1, 1]);
} else {
outerArray.push([1]);
}

// will only run if we had number greater than 2
if (num > 2) {
for (let i = 2; i < num; i++) {
let subArray = [];
subArray.push(1);
for (let j = 0; j < outerArray[i - 1].length - 1; j++) {
subArray.push(outerArray[i - 1][j] + outerArray[i - 1][j + 1]);
}
subArray.push(1);
outerArray.push(subArray);
}
}

return outerArray;
};
var generate = function (numRows) {
const res = [[1]];

for (let i = 1; i < numRows; i++) {
res[i] = [];
for (let k = 0; k < i + 1; k++) {
res[i][k] = (res[i - 1][k] || 0) + (res[i - 1][k - 1] || 0);
}
}

return res;
};

0 comments on commit b695fdf

Please sign in to comment.