fork download
  1. /*
  2. Problem: Apartment Housemate Preferences
  3.  
  4. Place everyone without exceeding any apartment's capacity.
  5. true means the person wants housemates.
  6. false means the person wants to live alone.
  7. Return the maximum number of happy people, or -1 if housing is impossible.
  8.  
  9. n = number of people
  10. m = number of apartments
  11. Time: O(n + m log m)
  12. Auxiliary space: O(m)
  13. */
  14. function maxSatisfiedPeople(rooms, wantsHousemates) {
  15. const n = wantsHousemates.length;
  16.  
  17. let wantsToShare = 0;
  18. for (const wantsHousemate of wantsHousemates) {
  19. if (wantsHousemate) wantsToShare++;
  20. }
  21. const wantsToLiveAlone = n - wantsToShare;
  22.  
  23. // Keep the original ascending sort. The biggest apartment is at the end.
  24. const capacities = [...rooms].sort((a, b) => a - b);
  25. const m = capacities.length;
  26. const maxSolo = Math.min(n, m);
  27.  
  28. // prefix[k] is the combined capacity of the k largest apartments.
  29. // With zero apartments, the total capacity is zero.
  30. const prefix = [0];
  31.  
  32. // Count the apartments that have at least two rooms.
  33. let shareable = 0;
  34.  
  35. // Start at the same end as the old inner loop.
  36. // For capacities [1, 2, 4], we add 4, then 2, then 1.
  37. // That builds prefix [0, 4, 6, 7].
  38. for (let i = capacities.length - 1; i >= 0; i--) {
  39. const capacity = capacities[i];
  40. prefix.push(prefix[prefix.length - 1] + capacity);
  41.  
  42. if (capacity >= 2) shareable++;
  43. }
  44.  
  45. // Keep -1 if every solo count fails to house everyone.
  46. let best = -1;
  47.  
  48. for (let solo = 0; solo <= maxSolo; solo++) {
  49. // Reserve the "solo" smallest apartments for people living alone.
  50. const remaining = n - solo;
  51.  
  52. // All three limits count apartments:
  53. // 1. How many apartments are left after reserving the solo apartments.
  54. // 2. How many apartments have at least two rooms.
  55. // 3. How many apartments can each receive at least two people.
  56. const sharedApartments = Math.min(
  57. m - solo,
  58. shareable,
  59. Math.floor(remaining / 2)
  60. );
  61.  
  62. // This lookup replaces the old inner loop.
  63. // If sharedApartments is 2, prefix[2] gives the sum of the two
  64. // largest capacities. Those apartments are outside the solo group.
  65. const sharedCapacity = prefix[sharedApartments];
  66.  
  67. // Skip this plan if the remaining people can't all fit.
  68. if (sharedCapacity < remaining) continue;
  69.  
  70. // Match solo spots with people who want to live alone first.
  71. // Any shortage or surplus of solo spots makes that many people unhappy.
  72. const unhappy = Math.abs(solo - wantsToLiveAlone);
  73. const happy = n - unhappy;
  74. best = Math.max(best, happy);
  75. }
  76.  
  77. return best;
  78. }
  79.  
  80. // Ideone test harness. No input is needed.
  81. const tests = [
  82. {
  83. name: "One shared apartment and one solo apartment",
  84. rooms: [2, 1, 4],
  85. wantsHousemates: [false, true, true, true],
  86. expected: 4,
  87. },
  88. {
  89. name: "Both shared apartments are needed",
  90. rooms: [2, 3],
  91. wantsHousemates: [true, true, true, true],
  92. expected: 4,
  93. },
  94. {
  95. name: "Two people can share a four-room apartment",
  96. rooms: [4],
  97. wantsHousemates: [true, true],
  98. expected: 2,
  99. },
  100. {
  101. name: "One person wants to live alone but has to share",
  102. rooms: [2],
  103. wantsHousemates: [false, true],
  104. expected: 1,
  105. },
  106. {
  107. name: "A one-room apartment cannot house two people",
  108. rooms: [1],
  109. wantsHousemates: [true, true],
  110. expected: -1,
  111. },
  112. {
  113. name: "Everyone lives alone, so prefix[0] is enough",
  114. rooms: [1, 2],
  115. wantsHousemates: [false, false],
  116. expected: 2,
  117. },
  118. ];
  119.  
  120. for (const test of tests) {
  121. const actual = maxSatisfiedPeople(test.rooms, test.wantsHousemates);
  122. const result = actual === test.expected ? "PASS" : "FAIL";
  123.  
  124. console.log(`${result} | ${test.name} | expected ${test.expected}, got ${actual}`);
  125. }
Success #stdin #stdout 0.03s 5716KB
stdin
Standard input is empty
stdout
PASS | One shared apartment and one solo apartment | expected 4, got 4
PASS | Both shared apartments are needed | expected 4, got 4
PASS | Two people can share a four-room apartment | expected 2, got 2
PASS | One person wants to live alone but has to share | expected 1, got 1
PASS | A one-room apartment cannot house two people | expected -1, got -1
PASS | Everyone lives alone, so prefix[0] is enough | expected 2, got 2