Last active
May 24, 2018 16:53
-
-
Save MichaelDimitras/f80c5934647f657f8a87c2b8edfd7a5d 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
/** | |
* @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