Skip to content

881. Boats to Save People #196

Discussion options

You must be logged in to vote

We can use a two-pointer greedy strategy combined with sorting. Here's the detailed approach:

  1. Sort the Array:

    • First, sort the people array. Sorting helps us to easily pair the lightest and heaviest person together in one boat, if possible.
  2. Two Pointer Strategy:

    • Use two pointers: one starting from the lightest person (left), and the other starting from the heaviest person (right).
    • Try to pair the heaviest person (right) with the lightest person (left). If the sum of their weights is less than or equal to the limit, they can share the same boat. Move both pointers (left++ and right--).
    • If they cannot be paired, send the heaviest person alone on a boat and move only the right pointer (r…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 22, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants