救生艇

给定数组 people 。people[i]表示第 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 <= 5 * 104
  • 1 <= people[i] <= limit <= 3 * 104
class Solution {
    public int numRescueBoats(int[] people, int limit) {
        int num = 0;
        Arrays.sort(people);
        int light = 0, heavy = people.length - 1;
        while (light <= heavy) {
            //最轻的和最重的能一起过河
            if (people[light] + people[heavy] <= limit) {
                light++;
                heavy--;
                num++;
            } else {
                //超重,那优先让重的先走
                heavy--;
                num++;
            }
        }
        return num;
    }
}                        
posted on 2024-06-23 17:56  zhengbiyu  阅读(57)  评论(0)    收藏  举报