Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var maxEnvelopes = function(envelopes) {
- var count = 0;
- if (envelopes.length === 0) { return count; }
- count++;
- if (envelopes.length === 1) { return count; }
- //sort by width of each envelope, sort by heigth of each envelope for matching widths
- envelopes.sort((a, b) => {
- if (a[0] < b[0]) {
- return -1;
- }
- if (a[0] > b[0]) {
- return 1;
- }
- if (a[0] === b[0]) {
- if (a[1] < b[1]) {
- return -1;
- }
- if (a[1] > b[1]) {
- return 1;
- }
- if (a[1] === b[1]) {
- return 0;
- }
- }
- })
- //check in order of envelopes for next width that is >
- //then compare height too
- //if yes, count++ and reset the item to check against
- var current_item = envelopes[0];
- for (var i = 1; i < envelopes.length; i++) {
- if (envelopes[i][0] > current_item[0]) {
- if (envelopes[i][1] > current_item[1]) {
- count++;
- current_item = envelopes[i];
- }
- }
- }
- return count;
- };
- //recursion needed but didn't get this working
- // var recurse = function(envelopes) {
- // //base case, reach end of envelopes and return 0 or 1 based on size
- // //recurse case, test for each item in the array
- // //if match, then recurse with the rest of the array, count + return
- // var count = 0;
- // if (envelopes.length === 0) { return count; }
- // count++;
- // if (envelopes.length === 1) { return count; }
- // var current_item = envelopes[0];
- // var maxCount = count;
- // for (var i = 1; i < envelopes.length; i++) {
- // if (envelopes[i][0] > current_item[0]) {
- // if (envelopes[i][1] > current_item[1]) {
- // count += recurse(envelopes.slice(i));
- // }
- // }
- // if (count > maxCount) {
- // maxCount = count;
- // }
- // }
- // return maxCount;
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement