There are 1 main approaches to solve this problem:
Let's start by understanding the problem: 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.
Thinking Process: Since each boat can carry at most 2 people, we want to optimize how we pair people to minimize the number of boats. A greedy approach would be to try to pair the heaviest person with the lightest person, if possible.
If the heaviest person and the lightest person can fit in one boat (i.e., their combined weight is less than or equal to the limit), we put them in the same boat. Otherwise, the heaviest person must take a boat alone.
Intuition: By sorting the array and using two pointers (one at the beginning for the lightest person and one at the end for the heaviest person), we can efficiently implement this greedy approach.
The time complexity is dominated by the sorting operation, which takes O(n log n) time. The two-pointer traversal takes O(n) time.
We only use a constant amount of extra space regardless of the input size (ignoring the space required for sorting, which depends on the implementation).
12345678910111213141516171819function numRescueBoats(people, limit): // Sort the array in ascending order sort(people) // Initialize pointers and counter i = 0 // lightest person j = people.length - 1 // heaviest person 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
Understand different approaches to solve the rescue boat dispatcher problem and analyze their efficiency.
Let's start by understanding the problem: 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.
Thinking Process: Since each boat can carry at most 2 people, we want to optimize how we pair people to minimize the number of boats. A greedy approach would be to try to pair the heaviest person with the lightest person, if possible.
If the heaviest person and the lightest person can fit in one boat (i.e., their combined weight is less than or equal to the limit), we put them in the same boat. Otherwise, the heaviest person must take a boat alone.
Intuition: By sorting the array and using two pointers (one at the beginning for the lightest person and one at the end for the heaviest person), we can efficiently implement this greedy approach.
The time complexity is dominated by the sorting operation, which takes O(n log n) time. The two-pointer traversal takes O(n) time.
We only use a constant amount of extra space regardless of the input size (ignoring the space required for sorting, which depends on the implementation).
12345678910111213141516171819function numRescueBoats(people, limit): // Sort the array in ascending order sort(people) // Initialize pointers and counter i = 0 // lightest person j = people.length - 1 // heaviest person 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
There are 1 main approaches to solve this problem:
Let's start by understanding the problem: 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.
Thinking Process: Since each boat can carry at most 2 people, we want to optimize how we pair people to minimize the number of boats. A greedy approach would be to try to pair the heaviest person with the lightest person, if possible.
If the heaviest person and the lightest person can fit in one boat (i.e., their combined weight is less than or equal to the limit), we put them in the same boat. Otherwise, the heaviest person must take a boat alone.
Intuition: By sorting the array and using two pointers (one at the beginning for the lightest person and one at the end for the heaviest person), we can efficiently implement this greedy approach.
The time complexity is dominated by the sorting operation, which takes O(n log n) time. The two-pointer traversal takes O(n) time.
We only use a constant amount of extra space regardless of the input size (ignoring the space required for sorting, which depends on the implementation).
12345678910111213141516171819function numRescueBoats(people, limit): // Sort the array in ascending order sort(people) // Initialize pointers and counter i = 0 // lightest person j = people.length - 1 // heaviest person 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
Understand different approaches to solve the rescue boat dispatcher problem and analyze their efficiency.
Let's start by understanding the problem: 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.
Thinking Process: Since each boat can carry at most 2 people, we want to optimize how we pair people to minimize the number of boats. A greedy approach would be to try to pair the heaviest person with the lightest person, if possible.
If the heaviest person and the lightest person can fit in one boat (i.e., their combined weight is less than or equal to the limit), we put them in the same boat. Otherwise, the heaviest person must take a boat alone.
Intuition: By sorting the array and using two pointers (one at the beginning for the lightest person and one at the end for the heaviest person), we can efficiently implement this greedy approach.
The time complexity is dominated by the sorting operation, which takes O(n log n) time. The two-pointer traversal takes O(n) time.
We only use a constant amount of extra space regardless of the input size (ignoring the space required for sorting, which depends on the implementation).
12345678910111213141516171819function numRescueBoats(people, limit): // Sort the array in ascending order sort(people) // Initialize pointers and counter i = 0 // lightest person j = people.length - 1 // heaviest person 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