当前位置:网站首页>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;
}
}
边栏推荐
- Unityui aspect processing (induction and accumulation)
- Navicate reports an error access violation at address 00000000
- 终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了
- Printf function buffer problem
- Visual system design example (Halcon WinForm) -10. PLC communication
- JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链
- 周鸿祎:数字安全能力落后也会挨打
- 事务_基本演示和事务_默认自动提交&手动提交
- How to do well in enterprise system vulnerability assessment
- 被动收入:回归原始且安全的两种赚取方法
猜你喜欢

Interprocess communication

The interviewer asked: how to judge whether an element is in the visible area?

视觉系统设计实例(halcon-winform)-9.文字显示

终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了

图解 SQL,这也太形象了吧

Automatically configure SSH password free login and cancel SSH password free configuration script

FPGA timing constraint sharing 04_ Output delay constraint

STM32 - capacitive touch button experiment

股票买卖4

Stock trading 4
随机推荐
移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
How to do well in enterprise system vulnerability assessment
See "sense of security" in uncertainty Volvo asked in 2022
Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
NEFU119 组合素数【算术基本定理】
Disk troubleshooting of kubernetes node
Basic exercises of C language
DirectX 入门知识
Thread knowledge summary
NEFU117 素数个数的位数【素数定理】
If we were the developer responsible for repairing the collapse of station B that night
关于印发《深圳市工业和信息化局绿色制造试点示范管理暂行办法》的通知
The interviewer asked: how to judge whether an element is in the visible area?
通过VN1630/VN7640的I/O功能来确认电源设置电压的时间精确度
[medical industry] DICOM converter tools
Differences among CPU, GPU and NPU
log4j2 jdbc appender
What is tor? What is the use of tor browser update?
Tencent two sides: @bean and @component are used in the same class, what will happen?
STM32F103C8T6在Arduino框架下驱动SH1106 1.3“ IIC OLED显示