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

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

Example 2

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

Example 3

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

Constraints

  • 1 <= nums.length <= 5*10^4
  • 1 <= nums[i] <= target <= 3*10^4
Run ⌘' · Submit ⌘⏎