码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 781. Rabbits in Forest

时间:2021-04-06 14:17:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:是的   new   rem   i++   ber   etc   shm   链接   possible   

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:

  1. answers will have length at most 1000.
  2. Each answers[i] will be an integer in the range [0, 999].

森林中的兔子。

森林中,每个兔子都有颜色。其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色。我们将这些回答放在 answers 数组里。

返回森林中兔子的最少数量。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rabbits-in-forest
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题我提供两种思路,一种需要排序,另一种不需要排序但是需要一个hashmap。

题目只给了一个例子的解释,所以并不能完全表达题目的意思,我第一次做的时候只是以为相同的数字代表的是相同颜色的兔子,但是实际不是的。我解释一下,比如第二个例子,有三个 10,但是为什么结果是 11 呢,那是因为这里input给了三个兔子看到的颜色,这三个兔子都看到了除了自己以外,有10个和自己颜色相同的兔子。但是如果给 [3,3,3,3,3] 这样的例子,结果就不是4了,因为这里有 5 个兔子看到了跟自己颜色相同的兔子。如果按照之前的逻辑,第一只兔子回答3,那么有4只这种颜色的,所以我们最多可以容纳4个回答3的兔子为同一颜色,若把第五只回答3的兔子也归为同色的,那就有5种这种颜色的兔子了,那它们看到的就不会是3而是4了,那么就矛盾了。所以此情形最少是8只。

排序的做法如下,先对input排序,排序之后,相同的数字会靠在一起,这样我们就很好分辨到底是不是同色了。一开始我们遍历到第一个数字的时候,如果这个数字为X,我们就要把指针向前再走X步,因为对于当前这只兔子来说,应该还有X只兔子跟他同色;但是如果在走完X步之后还有相同的数字,那么就说明一定是另外一种颜色。

时间O(nlogn)

空间O(1)

Java实现

 1 class Solution {
 2     public int numRabbits(int[] answers) {
 3         // corner case
 4         if (answers == null || answers.length == 0) {
 5             return 0;
 6         }
 7 
 8         // normal case
 9         int res = 0;
10         int max = 0;
11         Arrays.sort(answers);
12         for (int i = 0; i < answers.length; i++) {
13             res += answers[i] + 1;
14             max = answers[i] + 1;
15             int k = answers[i];
16             int start = i;
17             while (i < answers.length && k == answers[i] && i - start < max) {
18                 i++;
19             }
20             i--;
21         }
22         return res;
23     }
24 }

 

hashmap的做法其实也很类似,因为用了额外空间的缘故,我们不需要对input数组排序。如果当前兔子看到的是0,那么说明没有其他的兔子跟当前的兔子同色,就直接res++;如果当前这只兔子看到的数量不在hashmap中,则res += a + 1,说明起码有a + 1只兔子且应该是一个新的颜色;如果当前兔子看到的数量存在于hashmap中,我们就累加,比如我们看到一个10,也许当前这只兔子就是这11只兔子中间的一个;但是如果10被看到的次数大于11次了,那么说明肯定是一个别的颜色了,需要把10从hashmap中移除。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int numRabbits(int[] answers) {
 3         // corner case
 4         if (answers == null || answers.length == 0) {
 5             return 0;
 6         }
 7 
 8         // normal case
 9         HashMap<Integer, Integer> map = new HashMap<>();
10         int res = 0;
11         for (int a : answers) {
12             if (a == 0) {
13                 res += 1;
14                 continue;
15             }
16             if (!map.containsKey(a)) {
17                 map.put(a, 0);
18                 res += a + 1;
19             } else {
20                 map.put(a, map.getOrDefault(a, 0) + 1);
21                 if (map.get(a) == a) {
22                     map.remove(a);
23                 }
24             }
25         }
26         return res;
27     }
28 }

 

LeetCode 题目总结

[LeetCode] 781. Rabbits in Forest

标签:是的   new   rem   i++   ber   etc   shm   链接   possible   

原文地址:https://www.cnblogs.com/cnoodle/p/14615604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!