题目如下:

解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近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. CSS镜像

    https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scaleX transform: scaleX(-1);/*左 ...

  2. C# 内存建表备忘

    #region=====建表===== DataSet dataSet; // 创建表 DataTable table = new DataTable("testTable"); ...

  3. Ajax 实现导出文件-支持批量

    个人感觉前端不行,好多东西记不住,所以只能将遇到的坎以及解决方案记录下,方便以后用到时查找. 首先:ajax不支持流,网上找了好多版本,感觉下面的方案不错,实验了下可行. 前端页面: <div ...

  4. 转战 rocketmq

    接触 kafka 有一段时间了,一个人的力量实在有限,国内 rocketmq 的生态确实更好,决定换方向. rocketmq 文档地址:http://rocketmq.cloud/zh-cn/docs ...

  5. DT时代,企业更需构建精准数据分析体系

    DT时代,企业更需构建精准数据分析体系 随着互联网的飞速发展,信息的传输日益方便快捷,需求也日益突出,纵观整个互联网领域,大数据已被认为是继云计算.物联网之后的又一大颠覆性的技术性革命,毋庸置疑,大数 ...

  6. Http中Content-Type的取值讲解

    一.Content-Type的取值 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息(MediaType,即是Internet Media Type,互联网媒体类型: ...

  7. SpringMvc参数绑定出现乱码解决方法

    在SpringMvc参数绑定过程中出现乱码的解决方法 1.post参数乱码的解决方法 在web.xml中添加过滤器 <!-- 过滤器 处理post乱码 --> <filter> ...

  8. D Makoto and a Blackboard

    Makoto and a Blackboard time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. OPECV的配置

    因为要做一点道路是别的东西,所以想用到OPENCV的一些东西.在网上找了一些OPENCCSHARP的代码,但是这方面的书籍或者资料还是不是特别的多,所以我就觉得可能还不是很好.主要的原因还是因为自己的 ...

  10. ES6——面向对象应用

    面向对象应用——React 特点:     1.组件化(模块化) --- class(一个组件就是一个class)     2.强依赖与JSX (JSX==babel==browser.js  是JS ...