#881

Boats to Save People

specialist · 685 · lc medium +30 · verified · 61.4% accepted · 6,906 likes · top 61%

Description

You are given an array people representing the weights of individuals, and an integer limit giving the maximum weight each boat can carry. Each boat holds at most two people, subject to the weight limit.

Return the minimum number of boats required to carry everyone.

Example 1:

Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)

Example 2:

Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Code

1
2
3