题目如下:

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

    Linux系统处于一个高并发的运行环境,不管是系统调用还是中断都要求可重入,但是有一些系统资源处于临界区,因此,必须保证临界区资源访问的原子性. 对于临界区资源被占用时,发起访问的进程,有三种处理方法 ...

  2. Linux内核调试方法总结之ftrace

    ftrace [用途] ftrace包含一系列跟踪器,用于不同的场合,比如跟踪内核函数调用(function tracer).跟踪上下文切换(sched_switch tracer).查看中断被关闭的 ...

  3. 查询数据库对象的DDL信息

    表的DDL select dbms_metadata.get_ddl('HBBL','TABLE','AGREEMENT_MIRROR') from dual:

  4. __main__ — Top-level script environment

    w 29.4. __main__ — Top-level script environment — Python 3.6.1 documentation  https://docs.python.or ...

  5. Golang new() vs make()

    对于Golang的new() 和 make()的用法有些混乱,感觉这篇资料讲解较好,翻译一下,方便学习! 原文地址:http://www.godesignpatterns.com/2014/04/ne ...

  6. 【opencv】opencv图像识别的一些基础的基础函数的使用方法

    import cv2 import numpy as np from matplotlib import pyplot as plt pic_path = "deal_with.png&qu ...

  7. [Python3 填坑] 011 元组中有多个最值,索引出来的是哪一个

    目录 1. print( 坑的信息 ) 2. 开始填坑 (1) max() (2) min() (3) 结论 1. print( 坑的信息 ) 挖坑时间:2019/01/11 明细 坑的编码 内容 P ...

  8. MySql-8.0.16版本部分安装问题修正

    本帖参考网站<https://blog.csdn.net/lx318/article/details/82686925>的安装步骤,并对8.0.16版本的部分安装问题进行修正 在MySQL ...

  9. [常用类]排序及Arrays类(简单介绍)

    冒泡排序bubble sort  轻的上浮,重的下沉.两个相邻位置比较,如果前面元素比后面的元素大就换位置 选择排序 select sort 用一个索引上的元素,依次和其他位置上的元素比较,小的放前面 ...

  10. D Makoto and a Blackboard

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