2Brute2Best
🔥0

Boats to Save People

Medium·LeetCode #881 Pro OnlyInterview-Level
LeetCode
01

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.

Input

An integer array people and an integer limit.

Output

One integer: the minimum number of boats.

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

1 + 2 = 3 fits, so both share one boat.

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

Boats: [1, 2], [2], [3]. The 3 can't share with anyone, and the two 2s can't share with each other.

Example 3
Input: people = [3, 5, 3, 4], limit = 5
Output: 4

Every pair weighs more than 5, so everyone goes alone.

Constraints
  • • 1 ≤ people.length ≤ 5 · 10^4
  • • 1 ≤ people[i] ≤ limit ≤ 3 · 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“at most 2 people per boat”
→ Every boat is a pair (or a single) → a pairing problem, which suggests two pointers on a sorted array.
“minimum number of boats”
→ Make as many good pairs as possible. A greedy choice is needed for who pairs with whom.
“weight limit”
→ After sorting, the sum of the two ends tells us if the hardest person can be paired at all.
03

Approach Complexity Ladder

Brute Force — heaviest first, scan for a partnerO(N²)O(N)

Repeatedly find the heaviest person, then scan everyone for a partner who fits

Optimal — sort, then pair heaviest with lightestO(N log N)O(1)

Sort; each step the heaviest leaves, taking the lightest along if they fit

Brute Force Intuition

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:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.