【LeetCode】881. Boats to Save People 解题报告(Python)
【LeetCode】881. Boats to Save People 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/boats-to-save-people/description/
题目描述:
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
题目大意
一条船最多坐两个人,同时船有个载重,问最少需要多少条船才能装下所有人。
解题方法
如果感觉题目不好做的时候,建议再仔细读读题,题目说了最多坐两个人。。
方法比较简单,先排序,然后使用双指针。一个指向重的人,一个指向轻的人。题目说了一个人的重量不会超过载重。。
- 如果两个人能坐在同一条船上,那么把两个指针向中间移动
- 如果两个人的重量大于船的载重,那么让胖的人坐!因为瘦的人可以和别人挤挤,但是胖子不行啊,这个船必须都是他的了。(汽车的副驾驶2333)
- 重复上述操作
也就是说指向胖子的指针一定会移动,指向瘦子的指针只有在能坐下两个人的时候才能移动。
同时注意一下,循环的过程中,循环的条件中,允许hi == lo,因为这种情况下说明剩了一个人还没挤上船。
采用双指针,把每个人都遍历了一遍,所以时间复杂度是O(n),空间复杂度是O(1).
代码如下:
class Solution(object):
def numRescueBoats(self, people, limit):
"""
:type people: List[int]
:type limit: int
:rtype: int
"""
people.sort()
res = 0
hi, lo = len(people) - 1, 0
while hi >= lo:
if people[hi] + people[lo] <= limit:
lo += 1
hi -= 1
res += 1
return res
参考资料:
日期
2018 年 9 月 21 日 —— 转眼这个月又快过去了
【LeetCode】881. Boats to Save People 解题报告(Python)的更多相关文章
- [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 c ...
- LeetCode 881. Boats to Save People
原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
随机推荐
- LVS-三种模式的配置详情
NAT模式 实验环境 LVS1 VIP 192.168.31.66 DIP 192.168.121.128 WEB1 192.168.121.129 WEB2 192.168.121.130 安装与配 ...
- 完美png图片添加水印类
完美png图片添加水印类 被添加水印图片和水印图片都可以是png,保证透明无色背景,可调节透明度 <?phpclass Imgshuiyin{ /* 缩略图相关常量定义 */ const THU ...
- JAVA中复制数组的方法
在JAVA里面,可以用复制语句"A=B"给基本类型的数据传递值,但是如果A,B是两个同类型的数组,复制就相当于将一个数组变量的引用传递给另一个数组;如果一个数组发生改变,那么 引用 ...
- Swift-技巧(十) Protocol 的灵活使用
摘要 Protocol 是 Swift 中实现面向协议编程思想的重要部分.在使用过程中有遇到协议中声明的部分,但是在遵守部分不需要实现的,那么就需要使用 extension 参与进来,让 Protoc ...
- 【每天五分钟大数据-第一期】 伪分布式+Hadoopstreaming
说在前面 之前一段时间想着把 LeetCode 每个专题完结之后,就开始着手大数据和算法的内容. 想来想去,还是应该穿插着一起做起来. 毕竟,如果只写一类的话,如果遇到其他方面,一定会遗漏一些重要的点 ...
- win32汇编基础
win32汇编基础知识 Debug 版本|Release 版本 Debug 是"调试"的意思,Debug 版本就是为调试而生的,编译器在生成 Debug 版本的程序时会加入调试辅助 ...
- Stream.toMap
Collectors类的tomap方法将流收集到映射实例中. list 转 map collection.stream().collect(Collectors.toMap(User::getId, ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- Docker 生产环境之配置容器 - 自动启动容器
原文地址 Docker 提供了重启策略,以控制容器在退出时是否自动启动,或在 Docker 重新启动时自动启动.重启策略可确保链接的容器以正确的顺序启动.Docker 建议使用重启策略,并避免使用流程 ...
- java实现链式线性表
package ch9; public class LinkList <T>{ private class Node { //保存节点的数据 private T data; //指向下一个 ...