您的位置:首页 > 其它

Weekly Contest 71 leetcode 781. Rabbits in Forest

2018-02-11 14:46 197 查看
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 most 
1000
.
Each 
answers[i]
 will be an integer in the range 
[0,
999]
.
这道题也算简单,只不过要注意几个坑,要把测试用例想全面点。(具体见我代码中的测试用例)

package leetcode;

public class Rabbits_in_Forest_781 {

public int numRabbits(int[] answers) {
int[] map=new int[1000];
int rabbits=0;
for(int i=0;i<answers.length;i++){
if(answers[i]==0){
rabbits+=1;
}
else if(map[answers[i]]==0){
rabbits+=(answers[i]+1);
map[answers[i]]=1;
}
else{
if(map[answers[i]]<(answers[i]+1)){
map[answers[i]]++;
}
else{//map[answers[i]]已等于(answers[i]+1)了,不能再加了
rabbits+=(answers[i]+1);
map[answers[i]]=1;//重置
}
}
}
return rabbits;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Rabbits_in_Forest_781 r=new Rabbits_in_Forest_781();
int[] answers=new int[]{1,0,1,0,0};
int[] answers2=new int[]{0,0,1,1,1};
System.out.println(r.numRabbits(answers2));
}

}
这道题有solutions:https://leetcode.com/problems/rabbits-in-forest/solution/


Approach #1: Count [Accepted]

Intuition

每个有不同数字的兔子,它们的颜色肯定也不一样。因为一个兔子只会告诉我们关于它的颜色的信息。所以我们可以对于每个颜色的兔子来分开计数。

比如有 
13
 兔子的回答是 
5
 。这代表什么?假设其中一个兔子是红色,那么这当中就会有另外
5 只红色兔子,并且这 5 只兔子的回答也是 5 ,并且没有更多红色兔子了。所以,假设其中另外一个颜色是蓝色,那么这当中会有另外 5 只蓝色兔子。最后,假设另外一个颜色是绿色,那么会有另外 5 只绿色兔子,注意此时 13 只兔子不够了,说明其他的绿色兔子是在森林中,不在这 13 只中。 

所以答案是至少有 18 只兔子,因为至少有 3 种不同颜色,并且每种颜色有 6 只兔子。

Algorithm

总之, 如果有 
v
 只兔子的回答是 
k
,(其中 
v
= count[k]
 ),那么至少有 
a
 只兔子也会回答 
k
,(已经回答了/还在森林中),其中 
a
 是当 
a
>= v
 时, 
k+1
 的最小倍数。

之后我们将所有的 
a
 加到一起。

class Solution {
public int numRabbits(int[] answers) {
int res = 0;
int len = answers.length;
if (len == 0)
return 0;
Map<Integer, Integer> map = new HashMap<>();
for (int answer : answers) {
map.put(answer, map.getOrDefault(answer, 0) + 1);
}
for (Integer n : map.keySet()) {
int group = map.get(n) / (n + 1);
if (map.get(n) % (n + 1) == 0) {
res += group * (n + 1);
} else {
res += (group + 1) * (n + 1);
}
}
return res;
}
}

Complexity Analysis

Time Complexity: O(N)O(N),
where NN is
the number of rabbits that answered. In Java, our implementation using 
int[]
 instead
of a 
Map
 would be O(N
+ W)O(N+W),
where WW is
the number of possible different answers that rabbits could say.

Space Complexity: O(N)O(N).
In Java, our implementation is O(W)O(W).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: