Below is the implementation of the rescue boat dispatcher:
123456789101112131415161718192021222324252627282930313233343536/** * Greedy Approach with Two Pointers * Time Complexity: O(n log n) - Dominated by the sorting operation * Space Complexity: O(1) - Constant extra space * * @param {number[]} people - Array of people's weights * @param {number} limit - Maximum weight each boat can carry * @return {number} - Minimum number of boats needed */function numRescueBoats(people, limit) { // Sort the array in ascending order people.sort((a, b) => a - b); // Initialize pointers and counter let i = 0; // lightest person let j = people.length - 1; // heaviest person let boats = 0; // Process all people while (i <= j) { // If lightest and heaviest can share a boat if (people[i] + people[j] <= limit) { i++; // Move to next lightest person } j--; // Move to next heaviest person boats++; // Count this boat } return boats;} // Test casesconsole.log(numRescueBoats([1, 2], 3)); // 1console.log(numRescueBoats([3, 2, 2, 1], 3)); // 3console.log(numRescueBoats([3, 5, 3, 4], 5)); // 4
Let's break down the implementation:
Implement the rescue boat dispatcher solution in different programming languages.
Below is the implementation of the rescue boat dispatcher in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536/** * Greedy Approach with Two Pointers * Time Complexity: O(n log n) - Dominated by the sorting operation * Space Complexity: O(1) - Constant extra space * * @param {number[]} people - Array of people's weights * @param {number} limit - Maximum weight each boat can carry * @return {number} - Minimum number of boats needed */function numRescueBoats(people, limit) { // Sort the array in ascending order people.sort((a, b) => a - b); // Initialize pointers and counter let i = 0; // lightest person let j = people.length - 1; // heaviest person let boats = 0; // Process all people while (i <= j) { // If lightest and heaviest can share a boat if (people[i] + people[j] <= limit) { i++; // Move to next lightest person } j--; // Move to next heaviest person boats++; // Count this boat } return boats;} // Test casesconsole.log(numRescueBoats([1, 2], 3)); // 1console.log(numRescueBoats([3, 2, 2, 1], 3)); // 3console.log(numRescueBoats([3, 5, 3, 4], 5)); // 4
First, understand that we need to find the minimum number of boats to rescue everyone, where each boat can carry at most 2 people and has a weight limit.
Sort the array of people's weights in ascending order to facilitate the greedy approach.
Set up two pointers: one at the beginning (lightest person) and one at the end (heaviest person).
Try to pair the lightest person with the heaviest person if their combined weight doesn't exceed the limit. Otherwise, the heaviest person takes a boat alone.
Increment the boat counter for each boat used, whether it carries one or two people.
Return the total number of boats needed to rescue everyone.
Verify the solution with the provided examples and edge cases.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the rescue boat dispatcher problem using a different approach than shown above.
If every pair of people (lightest with heaviest, second lightest with second heaviest, etc.) can share a boat, the minimum number of boats is n/2 (rounded up).
If no two people can share a boat (each person's weight is more than half the limit), each person needs their own boat.
If there's only one person, we need exactly one boat.
If some people's weight equals the limit, they must take a boat alone.
Below is the implementation of the rescue boat dispatcher:
123456789101112131415161718192021222324252627282930313233343536/** * Greedy Approach with Two Pointers * Time Complexity: O(n log n) - Dominated by the sorting operation * Space Complexity: O(1) - Constant extra space * * @param {number[]} people - Array of people's weights * @param {number} limit - Maximum weight each boat can carry * @return {number} - Minimum number of boats needed */function numRescueBoats(people, limit) { // Sort the array in ascending order people.sort((a, b) => a - b); // Initialize pointers and counter let i = 0; // lightest person let j = people.length - 1; // heaviest person let boats = 0; // Process all people while (i <= j) { // If lightest and heaviest can share a boat if (people[i] + people[j] <= limit) { i++; // Move to next lightest person } j--; // Move to next heaviest person boats++; // Count this boat } return boats;} // Test casesconsole.log(numRescueBoats([1, 2], 3)); // 1console.log(numRescueBoats([3, 2, 2, 1], 3)); // 3console.log(numRescueBoats([3, 5, 3, 4], 5)); // 4
Let's break down the implementation:
Implement the rescue boat dispatcher solution in different programming languages.
Below is the implementation of the rescue boat dispatcher in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536/** * Greedy Approach with Two Pointers * Time Complexity: O(n log n) - Dominated by the sorting operation * Space Complexity: O(1) - Constant extra space * * @param {number[]} people - Array of people's weights * @param {number} limit - Maximum weight each boat can carry * @return {number} - Minimum number of boats needed */function numRescueBoats(people, limit) { // Sort the array in ascending order people.sort((a, b) => a - b); // Initialize pointers and counter let i = 0; // lightest person let j = people.length - 1; // heaviest person let boats = 0; // Process all people while (i <= j) { // If lightest and heaviest can share a boat if (people[i] + people[j] <= limit) { i++; // Move to next lightest person } j--; // Move to next heaviest person boats++; // Count this boat } return boats;} // Test casesconsole.log(numRescueBoats([1, 2], 3)); // 1console.log(numRescueBoats([3, 2, 2, 1], 3)); // 3console.log(numRescueBoats([3, 5, 3, 4], 5)); // 4
First, understand that we need to find the minimum number of boats to rescue everyone, where each boat can carry at most 2 people and has a weight limit.
Sort the array of people's weights in ascending order to facilitate the greedy approach.
Set up two pointers: one at the beginning (lightest person) and one at the end (heaviest person).
Try to pair the lightest person with the heaviest person if their combined weight doesn't exceed the limit. Otherwise, the heaviest person takes a boat alone.
Increment the boat counter for each boat used, whether it carries one or two people.
Return the total number of boats needed to rescue everyone.
Verify the solution with the provided examples and edge cases.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the rescue boat dispatcher problem using a different approach than shown above.
If every pair of people (lightest with heaviest, second lightest with second heaviest, etc.) can share a boat, the minimum number of boats is n/2 (rounded up).
If no two people can share a boat (each person's weight is more than half the limit), each person needs their own boat.
If there's only one person, we need exactly one boat.
If some people's weight equals the limit, they must take a boat alone.