Skip to main content
Back to problems
#881
Medium Algorithms

Boats to save people

Array Two Pointers Greedy Sorting
61.4% acceptance
Feb 22, 2026
6900
178
You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn num_rescue_boats(mut people: Vec<i32>, limit: i32) -> i32 {
    people.sort_unstable();
    let mut lo = 0i32;
    let mut hi = people.len() as i32 - 1;
    let mut boats = 0;
    while lo <= hi {
      if people[lo as usize] + people[hi as usize] <= limit { lo += 1; }
      hi -= 1;
      boats += 1;
    }
    boats
  }
}