题目如下:

解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近limit。考虑到人的总数最大是50000,而每个人的体重最大是30000,因此会有很多人体重一样。这样可以用一个集合set保存体重,再用字典保存每个体重对应的人的数量,可以减少运算量。最后对set进行升序排序,再用双指针的方法,用low指针指向set头部,high指针指向set尾部,分别比较set[low]和set[high]的值,有如下情况:

a. set[low] + set[high] > limit: 表示set[high]的体重人只能一人坐一船,得出boats += dic[set[high]], high -= 1;

b. set[low] + set[high] <= high: 表示可以共坐一船,但这里又要考虑  dic[set[high]] 与  dic[set[low]] 的大小:

b1. dic[set[high]]  > dic[set[low]]  : 得出 boats += dic[set[low]], low += 1;

b2.dic[set[high]]  < dic[set[low]]  : 得出 boats += dic[set[high]], high -= 1;

b3:dic[set[high]]  == dic[set[low]]  : 得出 boats += dic[set[high]], high -= 1,low += 1;

最后当low == high的时候,表示只剩下相同体重的人,这时判断这些人能否共坐一船,得出最终boats的总数。

代码如下:

class Solution(object):
def numRescueBoats(self, people, limit):
"""
:type people: List[int]
:type limit: int
:rtype: int
"""
dic = {}
puniq = []
for i in people:
if i not in dic:
dic[i] = 1
puniq.append(i)
else:
dic[i] += 1
puniq.sort()
boats = 0
low = 0
high = len(puniq) - 1
while low < high:
if puniq[low] + puniq[high] > limit:
boats += dic[puniq[high]]
dic[puniq[high]] = 0
high -= 1
else:
if dic[puniq[low]] > dic[puniq[high]]:
boats += dic[puniq[high]]
dic[puniq[low]] -= dic[puniq[high]]
dic[puniq[high]] = 0
high -= 1
elif dic[puniq[low]] < dic[puniq[high]]:
boats += dic[puniq[low]]
dic[puniq[high]] -= dic[puniq[low]]
dic[puniq[low]] = 0
low += 1
else:
boats += dic[puniq[high]]
dic[puniq[high]] = 0
dic[puniq[low]] = 0
low += 1
high -= 1
if limit >= puniq[high]*2:
boats += ((dic[puniq[high]]/2) + (dic[puniq[high]]%2))
else:
boats += dic[puniq[high]]
#boats += dic[puniq[low]] return boats

【leetcode】885. Boats to Save People的更多相关文章

  1. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  2. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_6_Map集合遍历键值对方式

    增强for

  2. APlayer 媒体播放引擎

    APlayer媒体播放引擎 Windows平台的播放内核 封闭式 DirectShow 架构,不受系统解码环境干扰 全媒体文件格式支持 丰富的媒体文件传输协议(http/https/ftp/mms/r ...

  3. 【Android Studio安装部署系列】十三、Android studio添加和删除Module 2

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 新建.导入.删除Module是常见的操作,这里简单介绍下. 新建Module File——New——New Module... 选中 ...

  4. 关于 阿里云 的linux 安装 jdk和tomcat 中的问题(解压版的jdk和tomcat)

    一安装jdk 1:首先要申请一个服务器 2:其次官网下载jdk(按你的linux版本下载(我的目前是64)) 3:就是上传jdk 4:在 usr 文件夹下创建 java 文件夹 5:把jdk上传到ja ...

  5. 事件 on emit off 封装

    /* on 绑定 emit 触发 off 解绑 //存放事件 eventList = { key:val handle:[] } 1对多 on(eventName,callback); handle: ...

  6. javascript实现深浅拷贝

    深浅拷贝通常是对于引用数据类型进行的(数据类型为:对象(Object).数组(Array).函数(Function)) 浅拷贝: let obj = {id: 1, name: 2}; let new ...

  7. 在使用spring中的ContextConfiguration、test注解时出现的错误

    错误: 在使用测试注解时出现ContextConfiguration注解和test注解无法正常导包使用的编译异常,如图: 解决办法: 将pom.xml文件中以下依赖管理 中的<scope> ...

  8. Tomcat控制台

    一般在安装完成Tomcat之后,我们需要验证tomcat是否安装成功,在浏览器的url中输入:http://127.0.0.1:8080/,就会进入如下的页面(表示安装成功): 在上面的左侧顶部,有一 ...

  9. Java枚举enum关键字

    枚举的理解 枚举其实就是一个类,枚举类的实例是一组限定的对象 传统的方式创建枚举 [了解] 对比:单例类 1.构造器私有化 2.本类内部创建对象 3.通过public static方法,对外暴露该对象 ...

  10. jsp常问面试题集

    1.Servlet总结 在Java Web程序中,Servlet主要负责接收用户请求 HttpServletRequest,在doGet(),doPost()中做相应的处理,并将回应HttpServl ...