LeetCode 881 救生艇(贪心)

题目

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

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

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

思路

排序+双指针+贪心

每次尽量选取一个较小的和一个较大的组合共用一条船。

代码

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(),people.end());//排序,从小到大
        int left=0,right=people.size()-1,sum=0;//双指针移动
        while(left<=right){
            if(people[left]+people[right]<=limit){//贪心,尽量满足limit
                sum++;
                right--;
                left++;
            }
            else{//若超重,说明右指针处单独要一条船,则右指针左移
                sum++;
                right--;
            }
        }
        return sum;
    }
};

原文地址:https://www.cnblogs.com/ambassdor/p/12245486.html

时间: 2024-10-30 11:37:02

LeetCode 881 救生艇(贪心)的相关文章

LeetCode 135 Candy(贪心算法)

135. Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get mo

leetcode 881. Boats to Save People

使用一艘船救人,每次最多只能救两人,请问最少要几次 这是左右节点法. var numRescueBoats = function (people, limit) { people.sort((a, b) => a - b) var left = 0; var right = people.length - 1; var boats = 0, track = [] track.add = function (a) { this.push(JSON.stringify(a)) } while (le

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

Leetcode 贪心 Best Time to Buy and Sell StockII

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Leetcode 贪心 Best Time to Buy and Sell Stock

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to

[LeetCode] Jump Game II 贪心

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[C++]LeetCode: 77 Best Time to Buy and Sell Stock II (贪心算法)

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times)

【LeetCode】贪心 greedy(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单

[LeetCode]621. 任务调度器(贪心)

题目 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务.任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完.CPU 在任何一个单位时间内都可以执行一个任务,或者在待命状态. 然而,两个相同种类的任务之间必须有长度为?n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,或者在待命状态. 你需要计算完成所有任务所需要的最短时间. 示例 1: 输入: tasks = ["A",&q