Boats to Save People

Medium

Topics
ArrayGreedyTwo PointersSorting

You are given weights nums and a boat capacity target. Each boat carries at most two people whose combined weight is at most target. Return the minimum number of boats to carry everyone.

Example 1

1 + 2
Each chip is one boat.
Each chip is one boat’s load; boats carry at most two people.
Input:  people = [1,2], limit = 3
Output: 1

Example 2

1 + 2
Input:  people = [3,2,2,1], limit = 3
Output: 3

Example 3

1 + 2
Input:  people = [3,5,3,4], limit = 5
Output: 4

Constraints

  • 1 <= nums.length <= 5*10^4
  • 1 <= nums[i] <= target <= 3*10^4
Editorial — intuition, approach, complexity and pitfalls

Intuition

Each boat carries at most two people and at most limit total weight. Minimise boats.

Sort, then pair the lightest with the heaviest. The exchange argument is short and worth giving: the heaviest person must travel, and whoever shares their boat, at most one person can. Giving them the lightest available companion maximises the chance that the pairing is feasible at all, and never does worse — if the lightest cannot fit with the heaviest, nobody can, so the heaviest goes alone. Either way the heaviest leaves, the problem shrinks, and the argument repeats.

So two pointers from the ends: if the pair fits, both board; otherwise only the heavier does. Either way the right pointer always moves and a boat is always used.

Approach

  1. Sort ascending.
  2. While i <= j: if nums[i] + nums[j] <= limit, advance i; always retreat j and count a boat.

Dry Run

people = [3,2,2,1], limit = 3
sorted [1,2,2,3]

i=0 j=3   1+3 = 4 > 3   -> 3 goes alone      boats 1   j=2
i=0 j=2   1+2 = 3 <= 3  -> both board        boats 2   i=1 j=1
i=1 j=1   2+2 = 4 > 3, but i == j so this is
          one person alone                   boats 3   j=0

answer 3

The last step is the one to watch: when the pointers meet there is a single person left, and the condition still consumes a boat for them correctly because j always decrements.

Complexity

  • Time — O(n log n): the sort dominates; the sweep is linear.
  • Space — O(log n) in Java and C++, O(n) in Python, all of it the sort. The sweep holds two indices, so the sort is the only reason this is not O(1).

Common Pitfalls

  • Using i < j as the loop condition, which drops the final person when the pointers meet.
  • Advancing i unconditionally. The lightest person only boards if the pair fits.
  • Failing to decrement j on the "goes alone" branch, which loops forever.
  • Trying to pack boats greedily by weight without pairing from both ends — matching heavy with heavy wastes capacity, and matching light with light leaves the heavy ones to travel alone.
  • Claiming O(1) space while sorting.

Full Solution — all languages

class Solution:
    def numRescueBoats(self, nums, target):
        nums.sort()
        i, j, boats = 0, len(nums) - 1, 0
        while i <= j:
            if nums[i] + nums[j] <= target:
                i += 1
            j -= 1
            boats += 1
        return boats
Asked at IBM
SensAI
Beta
Tap the mic to stop voice mode