leetcode 881. Boats to Save People

leetcode 881. Boats to Save People

题目:

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.)

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
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Note:

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

解法:

暴力遍历法,从头开始往后遍历,累加法求结果,可以求解,但是这个题会超时;

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(),people.end());
        int total = 0 ; 
        int temp = 0 ; 
        int times ++ ;
        for(int i = 0 ; i < people.size() ; i ++)
        {
            if(temp + people[i] < limit && times != 2 )
            {
                times ++ ;
                temp += people[i] ;
                continue ;
            }
            else
            {
                times = 0 ;
                total ++ ;
                temp = 0 ;
                i -- ;
            }
        }
        return total ;
    }
};

在这里插入图片描述

所以选择换一种方法做;

我们可以这么思考,先对people进行排序,然后我们就可以得到一个升序的people序列,随后我们每次都将体重最轻跟体重最重的人求和,来判断是否船是否可以成功承受

  • 如果可以承载,那么证明我们可以用这艘船一次拉走两个人,也就同时将需要拉走的人数从头往后推 1 从尾部往前推 1
  • 如果不可以承载,那么我们只能用这艘船将最重的人拉走,最轻的人还留在当地,同时将需要拉走的人从尾往前推 1

这样就可以避免超时的问题。


代码:
class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(),people.end());
        int first = 0 ;
        int end = people.size() - 1;
        int total = 0 ;
        while(first <= end)
        {
            total ++ ;
            if(people[first]+people[end] <= limit)
            {
                first ++ ;
            }
            end -- ;
        }
        return total ;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值