Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param {number[][]} envelopes
- * @return {number}
- */
- // w,h
- //[[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3
- var maxEnvelopes = function(envelopes) {
- // order envelop by width
- var wSort = envelopes.sort(function(a, b){
- return a[0] - b[0];
- });
- // // check envelop with similar width and keep only the one with the samllest height
- // // for (let i = 0; i < wSort.length-1; i++){
- // // similar width
- // if (wSort[i][0] === wSort[i+1][0]){
- // // compare h
- // if (wSort[i][1] >= wSort[i+1][1]){
- // // discard taller
- // wSort.splice(i, 1);
- // i--;
- // } else {
- // // discard taller
- // wSort.splice(i+1, 1);
- // }
- // }
- // }
- for (let i = 0; i < wSort.length-1; i++){
- // if h of the second envelop <= h of the first envelop remove second
- // OR w is the same
- if (wSort[i][1] >= wSort[i+1][1] || wSort[i][0] === wSort[i+1][0]){
- wSort.splice(i+1, 1);
- i--;
- }
- }
- // if I can add an envelop on it keep going
- // if I can't record result and try with the second envelop
- // find the biggest array in all the record
- // var results = [];
- // var result = [];
- // //var nbr = 0;
- // var found = false;
- // for (let i = 0; i < wSort.length-1; i++){
- // // get 1rst envelop
- // result.push(wSort[i]);
- // for (let j = i; j < wSort.length-1; j++){
- // // find an envelop to put on it
- // if (wSort[j][1] < wSort[j+1][1] || wSort[j][0] < wSort[j+1][0]){
- // result.push(wSort[j+1]);
- // found = true;
- // break;
- // }
- // }
- // if (!found){
- // //mbr ++;
- // results.push(result);
- // }
- // }
- return wSort.length;
- };
- //maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]);
- //maxEnvelopes([[1,1],[1,1],[1,1]]);
- maxEnvelopes([[1,3],[3,5],[6,7],[6,8],[8,4],[9,5]]);
- //maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement