当前位置:网站首页>LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
2022-07-27 14:05:00 【押切徹】
1.Description
森林中,每个兔子都有颜色。其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色。我们将这些回答放在 answers 数组里。
返回森林中兔子的最少数量。
2.Example
示例:
输入: answers = [1, 1, 2]
输出: 5
解释:
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
设回答了 "2" 的兔子为蓝色。
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
因此森林中兔子的最少数量是 5: 3 只回答的和 2 只没有回答的。
输入: answers = [10, 10, 10]
输出: 11
输入: answers = []
输出: 0
3.Solution
1.我的题解
使用一个数组统计兔子们说的数字,a,b的含义可自己推导,比如说2的兔子有4个,则最少的情况是有3个兔子的颜色一样,a的意思就是有多少种颜色,b的意思就是剩下的那个兔子还有另外两个和它颜色一样。
class Solution {
public static int numRabbits(int[] answers) {
int[] nums = new int[1000];
for(int i=0;i<answers.length;i++) {
nums[answers[i]]++;
}
int sum = 0;
for(int i=0;i<answers.length;) {
if(nums[answers[i]]!=-1&&nums[answers[i]]>(answers[i]+1)) {
int a = nums[answers[i]]/(answers[i]+1);
int b = nums[answers[i]]%(answers[i]+1);
sum += a*(answers[i]+1);
if(b!=0) {
sum += answers[i]+1;
}
}else if(nums[answers[i]]!=-1&&nums[answers[i]]<=(answers[i]+1)){
sum += answers[i]+1;
}
//将计算过的数置为一个不会被用到的数-1,方便遍历的时候将其跳过
nums[answers[i]] = -1;
while(i<answers.length&&nums[answers[i]]==-1) {
i++;
}
}
return sum;
}
}
2.官方
使用了哈希表来统计兔子。向上取整转向下取整:
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> count = new HashMap<Integer, Integer>();
for (int y : answers) {
count.put(y, count.getOrDefault(y, 0) + 1);
}
int ans = 0;
for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
int y = entry.getKey(), x = entry.getValue();
//将向上取整转化为向下取整,证明见图片
ans += (x + y) / (y + 1) * (y + 1);
}
return ans;
}
}
边栏推荐
- 被动收入:回归原始且安全的两种赚取方法
- [popular science] the difference and connection between accuracy and resolution
- 软件产品第三方测试费用为什么没有统一的报价?
- Is it safe for Guosen Securities to open a mobile account? Is Zhongshan securities reliable
- Kubernetes 节点磁盘故障排查
- 【WORK】关于技术架构
- 这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你!
- 数据仓库项目从来不是技术项目
- Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
- Jmeter录制接口自动化
猜你喜欢

Passive income: return to the original and safe two ways to earn
![Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]](/img/48/dd0fa3c494c45f604b7a93c707b808.png)
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]

JS what is declaration in advance? The order of function and variable declaration in advance (the foreshadowing of execution context)

对话框管理器第三章:创建控件

Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)

Research on multi label patent classification based on pre training model

Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement

Thread knowledge summary

大家最想要的,最全的C语言知识点总结,还不赶紧学习

Graphical SQL is too vivid
随机推荐
C语言基础练习题目
Is there a regular and safe account opening platform for gold speculation
[work] about technical architecture
@Detailed explanation of repository
数据仓库项目从来不是技术项目
log4j2 jdbc appender
自动化配置SSH免密登录和取消SSH免密配置脚本
Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
Shell programming specifications and variables
STM32F103C8T6在Arduino框架下驱动ssd1306 0.96“ IIC OLED显示
Docker practical experience: deploy mysql8 master-slave replication on docker
巨形象的图解 SQL
FPGA timing constraint sharing 04_ Output delay constraint
NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
If we were the developer responsible for repairing the collapse of station B that night
图解 SQL,这也太形象了吧
Unity3d learning note 10 - texture array
MySQL save data prompt: out of range value for column error
Interprocess communication
多表查询_练习1&练习2&练习3