Skip to content

Instantly share code, notes, and snippets.

@MichaelDimitras
Last active May 24, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelDimitras/f80c5934647f657f8a87c2b8edfd7a5d to your computer and use it in GitHub Desktop.
Save MichaelDimitras/f80c5934647f657f8a87c2b8edfd7a5d to your computer and use it in GitHub Desktop.
/**
* @param {number[][]} envelopes
* @return {number}
*/
var maxEnvelopes = function(envelopes) {
let maxNested = 0;
let sorted = envelopes.slice(0).sort((a,b) => {
return (b[0] + b[1] - a[0] - a[1])
})
const isNestable = function(a, b) {
console.log(a, b, b[0] < a[0] && b[1] < a[1])
return b[0] < a[0] && b[1] < a[1]
}
const explore = function(index, count) {
if (count > maxNested) {
maxNested = count;
}
for (let j = index; j < sorted.length; j++) {
if (isNestable(sorted[index], sorted[j])) {
explore(j, count + 1);
}
}
}
for (let i = 0; i < sorted.length - maxNested; i++) {
explore(i, 1)
}
return maxNested
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment