Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /**
  2. * @param {number[][]} envelopes
  3. * @return {number}
  4. */
  5.  
  6. // w,h
  7. //[[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3
  8.  
  9. var maxEnvelopes = function(envelopes) {
  10.  
  11. // order envelop by width
  12. var wSort = envelopes.sort(function(a, b){
  13. return a[0] - b[0];
  14. });
  15.  
  16. // // check envelop with similar width and keep only the one with the samllest height
  17. // // for (let i = 0; i < wSort.length-1; i++){
  18.  
  19. // // similar width
  20. // if (wSort[i][0] === wSort[i+1][0]){
  21. // // compare h
  22. // if (wSort[i][1] >= wSort[i+1][1]){
  23. // // discard taller
  24. // wSort.splice(i, 1);
  25. // i--;
  26. // } else {
  27. // // discard taller
  28. // wSort.splice(i+1, 1);
  29. // }
  30. // }
  31.  
  32. // }
  33.  
  34.  
  35. for (let i = 0; i < wSort.length-1; i++){
  36. // if h of the second envelop <= h of the first envelop remove second
  37. // OR w is the same
  38. if (wSort[i][1] >= wSort[i+1][1] || wSort[i][0] === wSort[i+1][0]){
  39. wSort.splice(i+1, 1);
  40. i--;
  41. }
  42. }
  43.  
  44.  
  45. // if I can add an envelop on it keep going
  46. // if I can't record result and try with the second envelop
  47. // find the biggest array in all the record
  48.  
  49. // var results = [];
  50. // var result = [];
  51. // //var nbr = 0;
  52. // var found = false;
  53.  
  54. // for (let i = 0; i < wSort.length-1; i++){
  55.  
  56. // // get 1rst envelop
  57. // result.push(wSort[i]);
  58.  
  59. // for (let j = i; j < wSort.length-1; j++){
  60. // // find an envelop to put on it
  61. // if (wSort[j][1] < wSort[j+1][1] || wSort[j][0] < wSort[j+1][0]){
  62. // result.push(wSort[j+1]);
  63. // found = true;
  64. // break;
  65. // }
  66. // }
  67.  
  68. // if (!found){
  69. // //mbr ++;
  70. // results.push(result);
  71. // }
  72.  
  73. // }
  74.  
  75. return wSort.length;
  76. };
  77.  
  78. //maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]);
  79. //maxEnvelopes([[1,1],[1,1],[1,1]]);
  80. maxEnvelopes([[1,3],[3,5],[6,7],[6,8],[8,4],[9,5]]);
  81. //maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement