Last active
November 9, 2017 18:39
-
-
Save kennyxcao/049af2389a415f63d2c73e894ae08f4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
// Edge Cases | |
if ( envelopes === null || envelopes.length === 0) { | |
return 0; | |
} | |
if ( envelopes.length === 1) { | |
return 1; | |
} | |
envelopes = envelopes.slice().sort(function (a, b) { | |
if (a[0] > b[0]) { | |
return 1; | |
} | |
if (a[0] < b[0]) { | |
return -1; | |
} | |
return a[1] - b[1]; | |
}); | |
var result = new Array(envelopes.length); | |
var max = 1; | |
var i, j; | |
for (i = 0; i < envelopes.length; i++) { | |
result[i] = 1; | |
for (j = 0; j < i; j++) { | |
if ( (envelopes[j][0] < envelopes[i][0]) && (envelopes[j][1] < envelopes[i][1])) { | |
result[i] = Math.max(result[i], result[j] + 1); | |
} | |
} | |
if (max < result[i]) { | |
max = result[i]; | |
} | |
} | |
return max; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment