Created
July 13, 2017 16:58
-
-
Save austinreuter/6d54d6b67e88fd65caccf5a726902428 to your computer and use it in GitHub Desktop.
Pascal's triangle interview q
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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