Implement the boatsToSavePeopleCount method that returns the minimum boats needed under the weight limit.
The input contains an array people, its length size, and a boat weight limit. Each value in the array represents the weight of one person.
Each boat can carry at most two people, and the total weight in a boat must not exceed limit. Your task is to return the minimum number of boats required to rescue everyone.
For example, for [3,2,2,1] with limit 3, the minimum number of boats is 3.
Use a greedy strategy after sorting the weights.
The heaviest remaining person must be placed in a boat. To save boats, try to pair that person with the lightest remaining person. If both fit within the limit, send them together. Otherwise, the heaviest person must go alone.
This works because pairing the heaviest person with the lightest possible partner gives the best chance of using fewer boats.
Pseudocode:
function boatsToSavePeopleCount(people, size, limit):
sort people in ascending order
left = 0
right = size - 1
boats = 0
while left <= right:
if people[left] + people[right] <= limit:
left++
right--
boats++
return boats