Created
September 21, 2017 16:29
-
-
Save tvolk131/e04913ca96ca143fb307ff978964647d to your computer and use it in GitHub Desktop.
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
let russianDoll = (envelopes) => { | |
let maxCount = 0; | |
let indicesUsed = {}; | |
let russianRecurse = (currentCount = 0, lastEnv = [-Infinity, -Infinity]) => { | |
if (currentCount > maxCount) { | |
maxCount = currentCount; | |
} | |
envelopes.forEach((envelope, index) => { | |
if (!indicesUsed[index] && envelope[0] > lastEnv[0] && envelope[1] > lastEnv[1]) { | |
indicesUsed[index] = true; | |
russianRecurse(currentCount + 1, envelope); | |
delete indicesUsed[index]; | |
} | |
}); | |
}; | |
russianRecurse(); | |
return maxCount; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment