Advertisement

Boats to Save People - LeetCode 881 Solution

Boats to Save People - Complete Solution Guide

Boats to Save People is LeetCode problem 881, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array people where people[i] is the weight of the i th 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 . Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanati

Detailed Explanation

The problem asks us to find the minimum number of boats needed to save all people, given an array `people` representing the weight of each person, and a `limit` representing the maximum weight a boat can carry. Each boat can carry at most two people, as long as their combined weight does not exceed the `limit`. The input is an array of integers `people` and an integer `limit`. The output is an integer representing the minimum number of boats required.

Solution Approach

The solution uses a greedy approach combined with sorting and the two-pointer technique. First, the `people` array is sorted in ascending order. This allows us to efficiently pair the lightest and heaviest people. Two pointers, `left` and `right`, are initialized to the start and end of the sorted array, respectively. The algorithm iterates while `left` is less than or equal to `right`. In each iteration, it checks if the sum of the weights of the people at the `left` and `right` pointers is less than or equal to the `limit`. If it is, then both people can be placed in the same boat, and both pointers are moved. If it isn't, then the heaviest person (at the `right` pointer) must be placed in a boat alone, and only the `right` pointer is moved. In either case, the boat count is incremented.

Step-by-Step Algorithm

  1. Step 1: Sort the `people` array in ascending order.
  2. Step 2: Initialize `left` to 0 and `right` to `len(people) - 1`.
  3. Step 3: Initialize `boats` to 0.
  4. Step 4: While `left <= right`, do the following:
  5. Step 5: If `people[left] + people[right] <= limit`, increment `left`.
  6. Step 6: Decrement `right`.
  7. Step 7: Increment `boats`.
  8. Step 8: Return `boats`.

Key Insights

  • Insight 1: Sorting the `people` array allows us to efficiently pair the lightest and heaviest people to maximize boat utilization.
  • Insight 2: Using the two-pointer technique with a left and right pointer allows us to check for pairs efficiently after sorting.
  • Insight 3: The greedy approach of pairing the lightest person with the heaviest possible person is optimal for minimizing the number of boats.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Greedy, Sorting.

Companies

Asked at: Atlassian, Deutsche Bank, Docusign, Sigmoid, UiPath, Walmart Labs.