LC 781. Rabbits in Forest
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 most1000
.- Each
answers[i]
will be an integer in the range[0, 999]
.
Runtime: 4 ms, faster than 92.37% of C++ online submissions for Rabbits in Forest.
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int,int> mp;
for(auto& a : answers){
mp[a]++;
}
int ret = ;
for(auto it = mp.begin(); it!=mp.end(); it++){
int m = it->second % (it->first + );
if(m == ){
ret += it->second;
}else{
ret += it->first+ - m + it->second;
}
}
return ret;
}
};
LC 781. Rabbits in Forest的更多相关文章
- 【LeetCode】781. Rabbits in Forest 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 781. Rabbits in Forest (森林中的兔子)
题目标签:HashMap 题目给了我们一组数字,每一个数字代表着这只兔子说 有多少只一样颜色的兔子. 我们把每一个数字和它出现的次数都存入map.然后遍历map,来判断到底有多少个一样颜色的group ...
- [Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- [LeetCode] Rabbits in Forest 森林里的兔子
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- linux之getopts
在编写shell脚本中,经常要处理一些输入参数,在使用过程中发现getopts更加方便,能够很好的处理用户输入的参数和参数值. getopts用于处理用户输入参数,举例说明使用方法: while ge ...
- (十五)连接网络adb,android模拟器打开
第一步:Android开发板连接usb和网线 adb shell setprop service.adb.tcp.port 5555 adb shell stop adbdadb shell star ...
- win10家庭版设置移动热点出现“我们无法设置移动热点”
寝室wifi卡到爆炸, 买了一个360随身WiFi,可是360随身WiFi烧坏了 ...然后我就一个星期没玩游戏了 今天本来想开电脑的wifi试一试,结果发现无法设置热点 纳闷了 百度一下,发现都 ...
- Django_02_创建模型
一:ORM简介 ORM,全拼Object-Relation Mapping,中文意为对象-关系映射,是随着面向对象的软件开发方法发展而产生的. 面向对象的开发方法是当今企业级应用开发环境中的主流开发方 ...
- 详解mpstat等性能监测命令的使用
mpstat是Multiprocessor Statistics的缩写,是实时监控工具,报告与cpu的一些统计信息这些信息都存在/proc/stat文件中,在多CPU系统里,其不但能查看所有的CPU的 ...
- Python3下UnicodeDecodeError:‘ASCII’ codec cant decode..(128)
今天准备用Keras跑一下LeNet的程序,结果总是编码出错 源代码是2.7写的,编码格式是utf-8.然后尝试网上各种方法不适用,最后还是解决了 源代码: data = gzip.open(r'C: ...
- 前端知识体系:JavaScript基础-原型和原型链-理解原型设计模式以及 JavaScript中的原型规则
理解原型设计模式以及 JavaScript中的原型规则(原文地址) 1.原型对象:我们创建的每一个函数(JavaScript中函数也是一个对象)都有一个原型属性 prototype,原型属性实质上是一 ...
- 4-windows启用账户锁定计数器
1.打开本地策略编辑器 命令:gpedit.msc 2.找到账户锁定策略 3.右键属性,设置登录无效次数 注:这个策略修改完后,不需要重新服务器就能生效
- js 获取json对象的 键 和 值
直接上图 结果:
- 在maven项目中如何引入另外一个项目(转)
原文链接:https://blog.csdn.net/jianfpeng241241/article/details/52654352 1 在Myeclipse中准备两个maven demo. , ...