Skip to content

Instantly share code, notes, and snippets.

@tvolk131
Created September 21, 2017 16:29
Show Gist options
  • Save tvolk131/e04913ca96ca143fb307ff978964647d to your computer and use it in GitHub Desktop.
Save tvolk131/e04913ca96ca143fb307ff978964647d to your computer and use it in GitHub Desktop.
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