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:
answerswill 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的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- STM32写选项字节(option bytes)的正确姿势
STM32 的 Flash information block 部分,包含有特殊的选项字节,可以用于系统配置等信息, 其中还有两个有效字节(实际四个字节,两个是校验字节)的用户自定义数据字节. 在尝试 ...
- 不创建父窗体的情况下他其他窗体的信息显示在第一个打开的窗体!(winfrom)
公司使用vs2008做的东西,用vs2017都打不开了(编译错误) 叫我更新一下,我看了一下,08的项目 和 winform 差不多 如何就用winfrom来做了 (winform 很久没碰了,, ...
- kubernetes之node隔离与恢复
需求: 在硬件升级, 硬件维护等情况下, 我们需要将某些node隔离, kubectl cordon <node_name> #禁止pod调度到该节点上, 在其上运行的pod并不会自动停止 ...
- fixed固定元素
1.css <style type="text/css"> .elementFixed{ position: fixed; top:0; } </style> ...
- 构建官方example
开发工具的选择: 构建官方example:
- 《SVG精髓》笔记(一)
本文是基于<SVG精髓>一书的简单总结,文中的demo均为该书提供,目的是方便大家使用时快速查阅. 1. 坐标系统 视口(viewport):文档使用的画布区域,表示SVG可见区域的大小, ...
- SpringBoot集成Druid实现监控
application.properties文件完整信息 #连接数据库 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spri ...
- windows下常用cmd命令
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr. ...
- Windows 刷新系统图标缓存
rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem 清理系统图标缓存数据库 attrib -h -s -r "%userpr ...
- C语言--变量
unsigned int 和 int 对计算机来讲没有区别, 只有在输出的时候, 计算机根据%d 和 %u 判断是否有符号位. %d 输出有符号的整数, %u 输出无符号整数. 无符号的整数比有符号的 ...