Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

31. Boats to Save People

MediumDay 9 of 60Two Pointers

The array people holds the weight of each person, and every boat can carry at most limit weight and at most two people at a time. No single person is heavier than limit. Return the minimum number of boats needed to carry everyone.

Example 1
Input: people = [3,2,2,1], limit = 3
Output: 3
Boats: (1, 2), (2) and (3).
Example 2
Input: people = [3,5,3,4], limit = 5
Output: 4
No two people fit together, so each needs a boat.
Constraints
  • 1 <= people.length <= 5 * 10^4
  • 1 <= people[i] <= limit <= 3 * 10^4
Hints
Frequently asked at: Amazon, Google, Flipkart
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min