fork download
  1. /*
  2. Problem: Apartment Housemate Preferences
  3.  
  4. Place everyone in an apartment without exceeding its room capacity.
  5. A person with preference true is happy if they have at least one
  6. housemate. A person with preference false is happy if they live alone.
  7. Apartments may stay empty. Return the maximum number of happy people.
  8.  
  9. TC: O(n + m²)
  10. SC: O(m) auxiliary space
  11.  
  12. n = number of people
  13. m = number of apartments
  14. */
  15. function maxSatisfiedPeople(rooms, wantsHousemates) {
  16. const n = wantsHousemates.length;
  17. const wantsToShare = wantsHousemates.filter(Boolean).length;
  18. const wantsToLiveAlone = n - wantsToShare;
  19.  
  20. const capacities = [...rooms].sort((a, b) => a - b);
  21. const maxSolo = Math.min(n, capacities.length);
  22.  
  23. // Return -1 if there is no way to house everyone.
  24. let best = -1;
  25.  
  26. for (let solo = 0; solo <= maxSolo; solo++) {
  27. // "solo" is how many people we choose to place alone in this plan.
  28. // We reserve the "solo" smallest apartments for them.
  29. const remaining = n - solo;
  30.  
  31. // Start fresh for each value of solo.
  32. let sharedCapacity = 0; // Total rooms in the apartments counted for sharing
  33. let sharedApartments = 0; // Number of apartments counted for sharing
  34.  
  35. // Start at the largest apartment. Indices below "solo" are reserved.
  36. for (let i = capacities.length - 1; i >= solo; i--) {
  37. const capacity = capacities[i];
  38.  
  39. // A shared apartment needs at least two rooms.
  40. // All apartments further left are this small or smaller.
  41. if (capacity < 2) break;
  42.  
  43. // Check BEFORE adding this apartment.
  44. // If we've counted one, adding this one would give us two.
  45. // Two shared apartments need at least four people, two in each.
  46. // With only two people available, we break and the count stays at one.
  47. if (2 * (sharedApartments + 1) > remaining) break;
  48.  
  49. // The check passed. Count the apartment and add all its rooms.
  50. sharedApartments++;
  51. sharedCapacity += capacity;
  52.  
  53. // If we have four rooms and two people, both people already fit.
  54. // If we have three rooms and four people, try the next apartment.
  55. if (sharedCapacity >= remaining) break;
  56. }
  57.  
  58. // This plan needs enough shared rooms for everyone left.
  59. if (sharedCapacity < remaining) continue;
  60.  
  61. // Match solo spots to people who want to live alone first.
  62. // If solo is 0 and one person wants to live alone, one is unhappy.
  63. // If solo is 3 and one person wants to live alone, two are unhappy.
  64. const unhappy = Math.abs(solo - wantsToLiveAlone);
  65. const happy = n - unhappy;
  66.  
  67. best = Math.max(best, happy);
  68. }
  69.  
  70. return best;
  71. }
  72.  
  73. // Ideone test harness
  74. // true means wants housemates. false means wants to live alone.
  75. const tests = [
  76. {
  77. name: "One large shared apartment and one solo apartment",
  78. rooms: [2, 1, 4],
  79. wantsHousemates: [false, true, true, true],
  80. expected: 4,
  81. },
  82. {
  83. name: "Three rooms are too few, so add the two-room apartment",
  84. rooms: [2, 3],
  85. wantsHousemates: [true, true, true, true],
  86. expected: 4,
  87. },
  88. {
  89. name: "Two people can share one four-room apartment",
  90. rooms: [4],
  91. wantsHousemates: [true, true],
  92. expected: 2,
  93. },
  94. {
  95. name: "One person wants to live alone but has to share",
  96. rooms: [2],
  97. wantsHousemates: [false, true],
  98. expected: 1,
  99. },
  100. {
  101. name: "A one-room apartment cannot house two people",
  102. rooms: [1],
  103. wantsHousemates: [true, true],
  104. expected: -1,
  105. },
  106. ];
  107.  
  108. for (const test of tests) {
  109. const actual = maxSatisfiedPeople(test.rooms, test.wantsHousemates);
  110. const result = actual === test.expected ? "PASS" : "FAIL";
  111. console.log(`${result} | ${test.name} | expected ${test.expected}, got ${actual}`);
  112. }
Success #stdin #stdout 0.03s 5644KB
stdin
Standard input is empty
stdout
PASS | One large shared apartment and one solo apartment | expected 4, got 4
PASS | Three rooms are too few, so add the two-room apartment | expected 4, got 4
PASS | Two people can share one 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