Created
October 31, 2019 16:11
-
-
Save suejungshin/a240fef8fbbcf64f25907ad3b5521639 to your computer and use it in GitHub Desktop.
Suejung Shin Week 2 interview prompt
This file contains 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
/** | |
* @param {number} numRows | |
* @return {number[][]} | |
*/ | |
var generate = function(numRows) { | |
if (numRows === 0) { | |
return []; | |
} | |
if (numRows === 1) { | |
return [[1]]; | |
} | |
let almostDoneArray = generate(numRows - 1); | |
let almostDoneArrayLastElement = almostDoneArray[almostDoneArray.length-1] | |
let lastResultElement = []; | |
lastResultElement.push(1) | |
for (let i = 0; i < almostDoneArrayLastElement.length - 1; i++) { | |
lastResultElement.push(almostDoneArrayLastElement[i] + almostDoneArrayLastElement[i+1]) | |
} | |
lastResultElement.push(1); | |
almostDoneArray.push(lastResultElement); | |
return almostDoneArray; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment