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 (left < right) { //注意条件两次最多两人
                if (people[left] + people[right] <= limit) {
                    //装上left, right
                    track.add([people[left], people[right]])
                    left++;
                    right--
                    boats++;
                } else {
                    //只能装right
                    track.add([people[right]])
                    boats++
                    right--
                }
                if (right == left) {
                    track.add([people[right]])
                    //只能装left
                    boats++;
                }
            }
            console.log(track, 'only test, limit = ', limit)
            return boats;
        };

        //1,2,2, 3
        numRescueBoats([3, 2, 2, 1], 3)
        numRescueBoats([3, 5, 3, 4], 5)
        numRescueBoats([1, 1, 2, 4], 4)

原文地址:https://www.cnblogs.com/rubylouvre/p/12147761.html

时间: 2024-11-06 11:30:03

leetcode 881. Boats to Save People的相关文章

[LeetCode] 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

LeetCode 881 救生艇(贪心)

题目 第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都能被船载). 思路 排序+双指针+贪心 每次尽量选取一个较小的和一个较大的组合共用一条船. 代码 class Solution { public: int numRescueBoats(vector<int>& people, int limit) { sort(people.beg

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted

【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 个单

Summary: curated List of Top 75 LeetCode Questions to Save Your Time

Facebook / Eng tech lead Dec 30, 2018  68 Comments New Year Gift to every fellow time-constrained engineer out there looking for a job, here's a list of the best LeetCode questions that teach you core concepts and techniques for each category/type of

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

[leetcode] Maximum Product Subarray @ python

[leetcode] Latest added: 2014-09-23 Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Trick:

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题解日练--2016.7.16

日练三题,冰冻三尺非一日之寒. 今日题目:1.顶端迭代器:2.完美平方数:3.根节点到叶节点数字和. 今日摘录: 人生是一场旅程.我们经历了几次轮回,才换来这个旅程.而这个旅程很短,因此不妨大胆一些,不妨大胆一些去爱一个人,去攀一座山,去追一个梦--有很多事我都不明白.但我相信一件事.上天让我们来到这个世上,就是为了让我们创造奇迹 ---<大鱼海棠> 284. Peeking Iterator | Difficulty: Medium Given an Iterator class inter