Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access