bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 881. Boats to Save People

881. Boats to Save People

Leetcode Solutions

Greedy Approach with Two Pointers

  1. Sort the people array in ascending order.
  2. Initialize two pointers: i at the start (lightest person) and j at the end (heaviest person) of the array.
  3. Initialize a variable boats to count the number of boats used.
  4. While i is less than or equal to j: a. If people[i] + people[j] is less than or equal to limit, increment i (since the lightest person is now on a boat). b. Decrement j (since the heaviest person is now on a boat). c. Increment boats (a new boat is used).
  5. Return the value of boats.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...