【leetcode】885. Boats to Save People
题目如下:

解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近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的更多相关文章
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- TensorFlow 源码编译安装
## Install prerequisites (rhel) yum install numpy python-devel python-wheel python-mock ## Install B ...
- Linux内核调试方法总结之死锁问题分析
死锁问题分析 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进程本身是解决不了的,需要外在的推动,才能解决,最重要的是死锁不仅仅影响进程业务, ...
- find查找特殊权限用法
find查找特殊权限的用法 find選項與參數: 3. 與檔案權限及名稱有關的參數: -name filename:搜尋檔案名稱為 filename 的檔案: -size [+-]SIZE:搜尋比 S ...
- .net 4.5 Test Async Task 异步测试
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...
- log4j.properties 输出指定类日志
比如,我只要众多日志中,红色框的日志,则可以指定类:com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory 修改配置文件: 再次输出结果为:
- Linux负载均衡实现
配置之前清空所有服务器防火墙规则 iptables -F 关闭selinux: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开 ...
- ecshop后台增加模块菜单详细教程
我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...
- python控制流-导入模块
一.模块 1.含义 Python 程序可以调用一组基本的函数,这称为“内建函数”,包括你见到过的 print().input()和 len()函数.Python 也包括一组模块,称为“标准库”.每个模 ...
- hive DDL操作
1.创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path ...
- CSS浏览器兼容性
答题技巧:因为这个问题主要是看你经验,一般有了开发经验的都会遇到这样的坑,你只要说出几个来大致就可以了. 1.对齐文本和文本输入框 问题: 当input元素在设置了高时,在IE7.IE8.IE9下会出 ...