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

参考资料:

https://leetcode.com/problems/boats-to-save-people/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/10854377.html

时间: 2024-10-18 00:21:36

[LeetCode] Boats to Save People 渡人的船的相关文章

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

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】双指针 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 个单

泰坦尼克号电影 完整字幕 中英文对比 完整版

0,0:01:50.93,0:01:53.03Thirteen meters. You should see it. 距离13公尺 应该看得见了 0,0:02:05.73,0:02:08.03Okay, take her up and over the bow rail. 越过船头的栏杆 0,0:02:09.53,0:02:12.73Okay, Mir- 2, we're going over the bow. Stay with us. 和平二号 我们要到船头了 跟好 0,0:02:45.63

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