Fewest boats to carry everyone when each boat holds at most two people under a weight limit

Quick Overview

Given each person's weight and a per-boat weight limit, find the fewest boats needed to carry everyone when a boat holds at most two people. It tests greedy pairing on sorted input, the two-pointer pattern, and arguing why a greedy choice is optimal.

Fewest boats to carry everyone when each boat holds at most two people under a weight limit

Company: Razorpay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array `people` where `people[i]` is the weight of the `i`-th person, and an infinite number of boats where each boat can carry a maximum weight of `limit`. Each boat carries **at most two people** at the same time, provided the sum of the weight of those people is at most `limit`. Return the minimum number of boats needed to carry every given person. ### Function Signature ```python def num_rescue_boats(people: list[int], limit: int) -> int: ``` ### Constraints - `1 <= len(people) <= 5 * 10^4` - `1 <= people[i] <= limit <= 3 * 10^4` ### Examples ```text Input: people = [1, 2], limit = 3 Output: 1 ``` One boat carries both people, because their weights sum to 3, which equals the limit. ```text Input: people = [3, 2, 2, 1], limit = 3 Output: 3 ``` The person weighing 3 cannot share a boat with anyone, and the two people weighing 2 cannot share with each other. The boats are (1, 2), (2) and (3). ```text Input: people = [3, 5, 3, 4], limit = 5 Output: 4 ``` The two lightest people already weigh 6 together, which is over the limit, so no pair fits and everyone rides alone.

Overview: Given each person's weight and a per-boat weight limit, find the fewest boats needed to carry everyone when a boat holds at most two people. It tests greedy pairing on sorted input, the two-pointer pattern, and arguing why a greedy choice is optimal.

Razorpay logo
Razorpay
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array people where people[i] is the weight of the i-th person, and an infinite number of boats where each boat can carry a maximum weight of limit.

Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats needed to carry every given person.

Function Signature

def num_rescue_boats(people: list[int], limit: int) -> int:

Constraints

  • 1 <= len(people) <= 5 * 10^4
  • 1 <= people[i] <= limit <= 3 * 10^4

Examples

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

One boat carries both people, because their weights sum to 3, which equals the limit.

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

The person weighing 3 cannot share a boat with anyone, and the two people weighing 2 cannot share with each other. The boats are (1, 2), (2) and (3).

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

The two lightest people already weigh 6 together, which is over the limit, so no pair fits and everyone rides alone.