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
placed in an array.
Return the minimum number of rabbits that could be in the forest.
Note:
Each
这道题也算简单,只不过要注意几个坑,要把测试用例想全面点。(具体见我代码中的测试用例)
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/
Intuition
每个有不同数字的兔子,它们的颜色肯定也不一样。因为一个兔子只会告诉我们关于它的颜色的信息。所以我们可以对于每个颜色的兔子来分开计数。
比如有
5 只红色兔子,并且这 5 只兔子的回答也是 5 ,并且没有更多红色兔子了。所以,假设其中另外一个颜色是蓝色,那么这当中会有另外 5 只蓝色兔子。最后,假设另外一个颜色是绿色,那么会有另外 5 只绿色兔子,注意此时 13 只兔子不够了,说明其他的绿色兔子是在森林中,不在这 13 只中。
所以答案是至少有 18 只兔子,因为至少有 3 种不同颜色,并且每种颜色有 6 只兔子。
Algorithm
总之, 如果有
之后我们将所有的
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
of a
+ 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).
answersare
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 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
Mapwould 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).
相关文章推荐
- Weekly Contest 71 leetcode 780. Reaching Points
- Weekly Contest 71 leetcode 782. Transform to Chessboard
- Weekly Contest 71 leetcode 783. Minimum Distance Between BST Nodes
- LeetCode 71 Simplify Path
- LeetCode71——Simplify Path
- Weekly Contest 72 leetcode 784. Letter Case Permutation
- Excel Sheet Column Number 分类: Leetcode 2015-01-15 11:15 71人阅读 评论(0) 收藏
- 【LeetCode71-80】71目录简化72DP问题73巧妙利用0位置76通用substr问题精妙模板79DFS+回溯
- leetcode做题总结,题目Simplify Path 71
- leetcode 71: Substring with Concatenation of All Words
- [leetcode-71]Simplify Path(C)
- 【Leetcode】71 Simplify path 简化路径
- [Leetcode 60] 71 Simplify Path
- LeetCode 71 Simplify Path
- leetcode 71: Substring with Concatenation of All Words
- Weekly Contest 70 leetcode 777. Swap Adjacent in LR String
- [leetcode 71] Simplify Path
- Weekly Contest 72 leetcode 785. Is Graph Bipartite?
- 【特别重要!!!快排的partition思想】【leetcode题解】【M】【71】Kth Largest Element in an Array
- 71 leetcode - Permutation Sequence