【LeetCode】781. Rabbits in Forest 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/rabbits-in-forest/description/
题目描述
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those answers are placed in an array.
Return the minimum number of rabbits that could be in the forest.
Examples:
Input: answers = [1, 1, 2]
Output: 5
Explanation:
The two rabbits that answered "1" could both be the same color, say red.
The rabbit than answered "2" can't be red or the answers would be inconsistent.
Say the rabbit that answered "2" was blue.
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
Input: answers = [10, 10, 10]
Output: 11
Input: answers = []
Output: 0
Note:
- answers will have length at most 1000.
- Each answers[i] will be an integer in the range [0, 999].
题目大意
兔子会报出和自己颜色相同的兔子数量,求最少的兔子数。
解题方法
充分理解大神的思路:
If
x+1
rabbits have same color, then we getx+1
rabbits who all answerx
. nown
rabbits answerx
.
Ifn%(x+1)==0
, we needn/(x+1)
groups ofx+1
rabbits.
Ifn%(x+1)!=0
, we needn/(x+1) + 1
groups ofx+1
rabbits.
the number of groups is math.ceil(n/(x+1)) and it equals to (n+i)/(i+1) , which is more elegant.
翻译一下:
当某个兔子回答x的时候,那么数组中最多允许x+1个兔子同时回答x,那么我们统计数组中所有回答x的兔子的数量n:
若 n%(x+1)==0,说明我们此时只需要 n/(x+1) 组个数为x+1的兔子。
若 n%(x+1)!=0,说明我们此时只需要 n/(x+1) + 1 组个数为x+1的兔子。
那么这两种情况可以通过 ceil(n/(x+1)) 来整合,而这个值也等于 (n + x) / (x + 1).
可以理解为,对每个数字进行记数,如果其出现的次数n能整除(x+1),说明能够成一个颜色,所以x+1个兔子。如果不能整除的话,说明有n/(x+1) + 1
组兔子。然后一个简化计算的方案。
代码:
class Solution(object):
def numRabbits(self, answers):
"""
:type answers: List[int]
:rtype: int
"""
count = collections.Counter(answers)
print count
return sum((count[x] + x) / (x + 1) * (x + 1) for x in count)
C++代码如下:
class Solution {
public:
int numRabbits(vector<int>& answers) {
int res = 0;
unordered_map<int, int> m;
for (int a : answers) m[a]++;
for (auto a : m) {
res += (a.second + a.first) / (a.first + 1) * (a.first + 1);
}
return res;
}
};
日期
2018 年 3 月 6 日
2018 年 12 月 18 日 —— 改革开放40周年
【LeetCode】781. Rabbits in Forest 解题报告(Python & C++)的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode 781. Rabbits in Forest (森林中的兔子)
题目标签:HashMap 题目给了我们一组数字,每一个数字代表着这只兔子说 有多少只一样颜色的兔子. 我们把每一个数字和它出现的次数都存入map.然后遍历map,来判断到底有多少个一样颜色的group ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- Ubuntu16.04安装 2.8.5版本Ansible
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...
- C#数据库连接方式【简版】
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...
- python-3.x-生成器使用
生成器函数代码: 1 def gen(n): 2 i = 1; 3 sum = 0; 4 while i <= n: 5 ''' 6 方法体1 -- sum求和是1到9的和 7 yield su ...
- java运行报错 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
解决方法: 解决办法: 在项目的属性里设置jdk版本,方法是右击项目-->properties-->java compiler --> Enable project specific ...
- Hive相关知识点
---恢复内容开始--- 转载:Hive 性能优化 介绍 首先,我们来看看Hadoop的计算框架特性,在此特性下会衍生哪些问题? 数据量大不是问题,数据倾斜是个问题. jobs数比较多的作业运行效率相 ...
- nodejs-Cluster模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...
- C/C++ Qt 数据库与SqlTableModel组件应用
SqlTableModel 组件可以将数据库中的特定字段动态显示在TableView表格组件中,通常设置QSqlTableModel类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过QDat ...
- 2021广东工业大学新生赛决赛 L-歪脖子树下的灯
题目:L-歪脖子树下的灯_2021年广东工业大学第11届腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 比赛的时候没往dp这方面想(因为之前初赛和月赛数学题太多了啊),因此只往组合数学 ...
- oracle 拆分字符串
WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...
- 石墨文档Websocket百万长连接技术实践
引言 在石墨文档的部分业务中,例如文档分享.评论.幻灯片演示和文档表格跟随等场景,涉及到多客户端数据同步和服务端批量数据推送的需求,一般的 HTTP 协议无法满足服务端主动 Push 数据的场景,因此 ...