You are given an array people
where people[i]
is the weight of the ith
person, and an infinite number of boats where each boat can carry a maximum weight of limit
. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit
.
Return the minimum number of boats to carry every given person.
Example 1:
Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5)
Constraints:
1 <= people.length <= 5 * 104
1 <= people[i] <= limit <= 3 * 104
This problem can be efficiently solved using a greedy approach with two pointers. The core idea is to minimize the number of boats used by optimally pairing lighter and heavier people whenever possible.
Algorithm:
Sort: Sort the people
array in ascending order of their weights. This allows us to efficiently pair lighter and heavier people.
Two Pointers: Initialize two pointers, left
and right
, pointing to the beginning and end of the sorted people
array, respectively. left
represents the lightest person, and right
represents the heaviest.
Iteration: Iterate while left
is less than or equal to right
:
people[left]
) and the heaviest person (people[right]
) is less than or equal to the limit
.
people[left] + people[right] <= limit
: This means we can put both people in the same boat. Increment left
(move to the next lightest person) and decrement right
(move to the next heaviest person).people[left] + people[right] > limit
: This means the heaviest person (people[right]
) needs a boat by themselves. Only decrement right
.Boat Count: Increment the ans
(boat count) in each iteration. This represents using one boat in each step.
Return: After the loop finishes, ans
contains the minimum number of boats needed.
Time Complexity: O(n log n), dominated by the sorting step.
Space Complexity: O(log n) or O(1), depending on the sorting implementation. In-place sorting algorithms like quicksort or mergesort use logarithmic space for the recursive calls.
Code Implementation (Python):
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort() #Sort the people by weight
left = 0
right = len(people) - 1
ans = 0
while left <= right:
if people[left] + people[right] <= limit:
left += 1 #Both people fit in one boat
right -= 1 #Heaviest person always needs a boat, might be alone
ans += 1
return ans
Code Implementation (Java):
import java.util.Arrays;
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people); //Sort people array
int left = 0;
int right = people.length - 1;
int ans = 0;
while (left <= right) {
if (people[left] + people[right] <= limit) {
left++; //Both fit in one boat
}
right--; //Heaviest person always needs a boat
ans++;
}
return ans;
}
}
Code Implementation (C++):
#include <algorithm>
#include <vector>
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
std::sort(people.begin(), people.end()); //Sort people vector
int left = 0;
int right = people.size() - 1;
int ans = 0;
while (left <= right) {
if (people[left] + people[right] <= limit) {
left++; //Both fit in one boat
}
right--; //Heaviest person needs a boat
ans++;
}
return ans;
}
};
The other languages (Go, TypeScript) would follow similar structures, employing sorting and a two-pointer approach for optimal boat usage. The core logic remains consistent across all the languages.