881. Boats to Save People
Intuition
This problem will use two pointer to record the current limit is meet the limit or not and count the total boats we need.
Approach
- Sort the array of people's weights in ascending order.
- Use two pointers: one starting from the lightest person (left) and one from the heaviest person (right).
- For each iteration, try to pair the lightest and heaviest people:
- If their combined weight is within the limit, both can share a boat (increment left pointer).
- If their combined weight exceeds the limit, the heavier person must take a boat alone.
- In either case, the right pointer decreases and we count one more boat.
- Continue until all people are assigned to boats.
- If there's one person left (when left equals right), allocate one more boat.
Complexity
- Time complexity: O(NlogN)
- Space complexity: O(1)
Keywords
- Two Pointers
- Greedy Algorithm
- Sorting