Rabbits in Forest

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 de >answersde> 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:

  1. de >answersde> will have length at most de >1000de>.
  2. Each de >answers[i]de> will be an integer in the range de >[0, 999]de>.
采访了若干只兔子,这些兔子都回答除了自己之外,还有多少只兔子的颜色和自己相同,问森林里最少有多少只兔子。

很简单可以想到,回答相同的兔子有可能是相同颜色的兔子,但是要考虑到,如果有三只兔子回答1,那么这三只兔子不可能都是同一种颜色,如果是这样的话,这三只兔子都会回答2。从这个例子看出来,如果有n只兔子回答i,那么这n只兔子里最多有(i+ 1)只是同一种颜色,剩余的(n-i-1)只仍然服从此规则。统计所有的i和n,运用上述规则,得解。代码如下

class Solution {
    public int numRabbits(int[] answers) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int it : answers){
        	if(!map.containsKey(it))
        		map.put(it, 0);
        	map.put(it, map.get(it) + 1);
        }
        int res = 0;
        for(int key : map.keySet()){
        	int num = map.get(key);
        	while(num > 0){
        		res += key + 1;
        		num -= key + 1;
        	}
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面有篇英文课文,请编程找出课文中所有的单词,统计单词出现的次数,统计时不区分字母的大小写,最后按出现的次数从大到小显示出现3次以上各个单词及次数。例如,结果显示如下: the 18 a 14 puma 9 in 8 it 8 ...(略) 英文课文如下: Pumas are large, cat-like animals which are found in America. When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously. However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar. The hunt for the puma began in a small village where a woman picking blackberries saw 'a large cat' only five yards away from her. It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered. The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening. Wherever it went, it left behind it a trail of dead deer and small animals like rabbits. Paw prints were seen in a number of places and puma fur was found clinging to bushes. Several people complained of 'cat-like noises' at night and a businessman on a fishing trip saw the puma up a tree. The experts were now fully convinced that the animal was a puma, but where had it come from ? As no pumas had been reported missing from any zoo in the country, this one must have been in the possession of a private collector and somehow managed to escape. The hunt went on for several weeks, but the puma was not caught. It is disturbing to think that a dangerous wild animal is still at large in the quiet countryside.
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值