当前位置:网站首页>Leetcode 781. rabbit hash table in forest / mathematical problem medium
Leetcode 781. rabbit hash table in forest / mathematical problem medium
2022-07-27 15:28:00 【Abcheche】
List of articles
1.Description
In the forest , Every rabbit has a color . Some of the rabbits ( It could be all ) Tell you how many other rabbits have the same color as themselves . We put these answers in answers In the array .
Return to the minimum number of rabbits in the forest .
2.Example
Example :
Input : answers = [1, 1, 2]
Output : 5
explain :
Two answered "1" The rabbit of may have the same color , Set it to red .
Then I answered "2" The rabbit is not red , Otherwise their answers will contradict each other .
Let's answer "2" The rabbit is blue .
Besides , There should be something else in the forest 2 Only the blue rabbit's answer is not included in the array .
So the minimum number of rabbits in the forest is 5: 3 Only answers and 2 There is no answer .
Input : answers = [10, 10, 10]
Output : 11
Input : answers = []
Output : 0
3.Solution
1. My explanation
Use an array to count the numbers that rabbits say ,a,b You can deduce the meaning of , for instance 2 The rabbit has 4 individual , Then at least there is 3 The two rabbits are the same color ,a It means how many colors there are ,b It means that there are two other rabbits with the same color as it .
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;
}
// Set the calculated number to a number that will not be used -1, It is convenient to skip it when traversing
nums[answers[i]] = -1;
while(i<answers.length&&nums[answers[i]]==-1) {
i++;
}
}
return sum;
}
}
2. official
A hash table is used to count rabbits . Round up to round down :
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();
// Convert rounding up to rounding down , See the picture for the certificate
ans += (x + y) / (y + 1) * (y + 1);
}
return ans;
}
}
边栏推荐
- Sword finger offer cut rope
- 3D相关的简单数学知识
- Code coverage statistical artifact -jacobo tool practice
- EMC design scheme of RS485 interface
- Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
- 网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
- 网络设备硬核技术内幕 路由器篇 18 DPDK及其前传(三)
- generic paradigm
- 网络设备硬核技术内幕 路由器篇 11 CISCO ASR9900拆解 (五)
- MOS管防止电源反接的原理
猜你喜欢

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

STM32 can communication filter setting problem

EMC design scheme of USB2.0 Interface

4种单片机驱动继电器方案

Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)

LeetCode 240. 搜索二维矩阵 II medium

reflex

LeetCode 783. 二叉搜索树节点最小距离 树/easy

Four kinds of relay schemes driven by single chip microcomputer
随机推荐
Dialog manager Chapter 3: create controls
Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
STM32学习之CAN控制器简介
Distributed lock
JUC(JMM、Volatile)
Lua study notes
Tools - common methods of markdown editor
Two stage submission and three stage submission
How to edit a framework resource file separately
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
网络设备硬核技术内幕 路由器篇 15 从鹿由器到路由器 (下)
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
With just two modifications, apple gave styleganv2 3D generation capabilities
Method of removing top navigation bar in Huawei Hongmeng simulator
网络设备硬核技术内幕 路由器篇 4 贾宝玉梦游太虚幻境(下)
泛型
The design method of integral operation circuit is introduced in detail
Four kinds of relay schemes driven by single chip microcomputer
reflex
Leetcode 190. reverse binary bit operation /easy