LeetCode 881 Boats to Save People (Python)

Posted by 小明MaxMing on January 13, 2020

题目

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 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. (It is guaranteed each person can be carried by a boat.)

解题思路

贪心,将数组从大到小排序,每次判断最大和最小的能否放到一个船里,如果可以放两个,不可以只放大的

代码

class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort(reverse=True)
        i, j = 0, len(people) - 1
        while i <= j:
            if people[i] + people[j] <= limit:
                j -= 1
            i += 1
        return i

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅