每日算法系列【LeetCode 881】救生艇

题目描述

第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit。

每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。

返回载到每一个人所需的最小船数。(保证每个人都能被船载)。

示例1

        输入:
people = [1,2], limit = 3
输出:
1
解释:
1 艘船载 (1, 2)
      

示例2

        输入:
people = [3,2,2,1], limit = 3
输出:
3
解释:
3 艘船分别载 (1, 2), (2) 和 (3)
      

示例3

        输入:
people = [3,5,3,4], limit = 5
输出:
4
解释:
4 艘船分别载 (3), (3), (4), (5)
      

提示

  • 1 <= people.length <= 50000
  • 1 <= people[i] <= limit <= 30000

题解

这题第一直觉就是,对于一个人 people[i] 来说,装了他之后船上还剩 limit-people[i] 的空间,所以第二个人要尽量装能装得下的人中最重的那个。

那如果直接这么遍历显然不行,时间复杂度是 O(n^2) 的。所以我们先对重量进行排序,然后从最轻的人开始看,如果他能和最重的坐一个船,那就去掉他们俩,在剩下的人里继续找;如果不能的话,那么最重的人只能单独坐船,然后看能不能和第二重的人坐一个船,依次下去。

具体实现上,只需要用两个指针,一个指着最小的数,一个指着最大的数就行了,时间复杂度是 O(n) ,但是由于还得调用一次快速排序,所以最终时间复杂度是 O(n \log n)

当然,因为这题重量范围比较小,所以如果采用计数排序,可以进一步降低时间复杂度。

代码

双指针(c++)

        class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());
        int l = 0, r = people.size() - 1, res = 0;
        while (l <= r) {
            res++;
            if (people[l] + people[r] <= limit) l++;
            r--;
        }
        return res;
    }
};

      

双指针(python)

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

计数排序(c++)

        class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        int n = people.size();
        vector<int> sp(n, 0);
        vector<int> count(limit+1, 0);
        for (int i = 0; i < n; ++i) {
            count[people[i]]++;
        }
        for (int i = 1; i <= limit; ++i) {
            count[i] += count[i-1];
        }
        for (int i = 0; i < n; ++i) {
            sp[count[people[i]]-1] = people[i];
            count[people[i]]--;
        }
        int l = 0, r = n - 1, res = 0;
        while (l <= r) {
            res++;
            if (sp[l] + sp[r] <= limit) l++;
            r--;
        }
        return res;
    }
};

      

计数排序(python)

        class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        sp = [0] * len(people)
        count = [0] * (limit + 1)
        for i in people:
            count[i] += 1
        for i in range(1, limit + 1):
            count[i] += count[i-1]
        for i in people:
            sp[count[i]-1] = i
            count[i] -= 1
        res, l, r = 0, 0, len(people) - 1
        while l <= r:
            res += 1
            if sp[l] + sp[r] <= limit:
                l += 1
            r -= 1
        return res
      

后记

这题注意写循环的时候也有技巧的,我们实现的时候,对于最大值 people[r] ,先给他分配一条船,再看最小的人能否和他一条船,如果可以,那就顺便带上( l 加 1 ),如果不可以的话,那就 r 减 1 ,继续看下一个更重的人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法码上来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值