Skip to content

Instantly share code, notes, and snippets.

@austinreuter
Created July 13, 2017 16:58
Show Gist options
  • Save austinreuter/6d54d6b67e88fd65caccf5a726902428 to your computer and use it in GitHub Desktop.
Save austinreuter/6d54d6b67e88fd65caccf5a726902428 to your computer and use it in GitHub Desktop.
Pascal's triangle interview q
var generate = function(numRows) {
if (numRows === 0) {
return [];
}
var output = [[1]];
if (numRows === 1) {
return output;
}
for (var i = 0; i < numRows; i++) {
var last = [];
output.forEach(function(prev) {
last[0] = prev[0];
for (var i = 0; i < prev.length; i++) {
if (prev[i + 1] === undefined) {
last[i + 1] = prev[i];
} else {
last[i + 1] = prev[i] + prev[i + 1];
}
}
output.push(last)
});
}
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment