Boats to Save People
Problem Statement
people[i] is the weight of person i. You have as many boats as you need. Every boat can carry at most 2 people, and the total weight in a boat must be at most limit.
Return the smallest number of boats needed to carry everyone.
No single person is heavier than limit, so everyone can at least go alone.
An integer array people and an integer limit.
One integer: the minimum number of boats.
1 + 2 = 3 fits, so both share one boat.
Boats: [1, 2], [2], [3]. The 3 can't share with anyone, and the two 2s can't share with each other.
Every pair weighs more than 5, so everyone goes alone.
- • 1 ≤ people.length ≤ 5 · 10^4
- • 1 ≤ people[i] ≤ limit ≤ 3 · 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Repeatedly find the heaviest person, then scan everyone for a partner who fits
Sort; each step the heaviest leaves, taking the lightest along if they fit
Keep a used array. Each round, scan for the heaviest person still on shore and put them in a new boat. Then scan everyone else and add the heaviest partner who still fits, so the boat is as full as possible. Repeat until everyone is on a boat.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.