★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10543038.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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: 11Input: 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 数组里。

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

示例:
输入: answers = [1, 1, 2]
输出: 5
解释:
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
设回答了 "2" 的兔子为蓝色。
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
因此森林中兔子的最少数量是 5: 3 只回答的和 2 只没有回答的。输入: answers = [10, 10, 10]
输出: 11输入: answers = []
输出: 0

说明:

  1. answers 的长度最大为1000
  2. answers[i] 是在 [0, 999] 范围内的整数。

Runtime: 20 ms
Memory Usage: 19.3 MB
 1 class Solution {
 2     func numRabbits(_ answers: [Int]) -> Int {
 3         var res:Int = 0
 4         var m:[Int:Int] = [Int:Int]()
 5         for ans in answers
 6         {
 7             m[ans,default:0] += 1
 8         }
 9         for (key,val) in m
10         {
11             res += (val + key) / (key + 1) * (key + 1)
12         }
13         return res
14     }
15 }


24ms

 1 class Solution {
 2     func numRabbits(_ answers: [Int]) -> Int {
 3         var counter = [Int: Int]()
 4         answers.forEach{ counter[$0, default: 0] += 1 }
 5
 6         var ret = 0
 7         for (n, c) in counter {
 8             let colors = (c + n) / (n + 1)
 9             ret += colors * (n + 1)
10         }
11         return ret
12     }
13 }


24ms

 1 class Solution {
 2     func numRabbits(_ answers: [Int]) -> Int {
 3         if answers.count == 0 {
 4         return 0
 5     }
 6
 7     let sortAns = answers.sorted()
 8     var first = -1
 9
10     var total = 0
11     var sep = 0
12
13     for (_, itemCount) in sortAns.enumerated() {
14         if itemCount != first || sep == first {
15             first = itemCount
16             sep = 0
17
18             total += 1 + itemCount
19         } else {
20             sep += 1
21         }
22     }
23     return total
24     }
25 }


48ms

 1 class Solution {
 2     func numRabbits(_ answers: [Int]) -> Int {
 3         var mark = [Int : Int]()
 4         for answer in answers {
 5             if let count = mark[answer] {
 6                 mark[answer] = count + 1
 7             } else {
 8                 mark[answer] = 1
 9             }
10         }
11
12         var result = 0
13         for (key, value) in mark {
14             let groupNum = key + 1
15             var group = value / groupNum
16             if value % groupNum != 0 {
17                 group += 1
18             }
19             result += group * groupNum
20         }
21         return result
22     }
23 }

转载于:https://www.cnblogs.com/strengthen/p/10543038.html

[Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest相关推荐

  1. Leetcode--781.森林中的兔子

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

  2. 森林中的兔子(超详细解析)

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

  3. leetcode 781. 森林中的兔子(hashmap)

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

  4. LeetCode 781. 森林中的兔子(哈希+贪心)

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

  5. leetcode每日一题—781.森林中的兔子

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

  6. 森林中的兔子,有感而抒

    力扣4月4日每日一题,传送门 有感而抒 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. 返回森林中兔子的最 ...

  7. leetcode 781. Rabbits in Forest | 781. 森林中的兔子(Java)

    题目 https://leetcode.com/problems/rabbits-in-forest/ 题解 另 answers 中的每一个元素称为 n,那么,n 最多能抵消 n+1 个同类元素. c ...

  8. 科研文献|根相关真菌群落反映了亚热带森林中宿主的空间共生模式

    TITLE:Root-associated fungal community reflects host spatial co-occurrence patterns in a subtropical ...

  9. R语言使用caret包构建随机森林模型(random forest)构建回归模型、通过method参数指定算法名称、通过ntree参数指定随机森林中树的个数

    R语言使用caret包构建随机森林模型(random forest)构建回归模型.通过method参数指定算法名称.通过ntree参数指定随机森林中树的个数 目录

最新文章

  1. 用git提交代码时,由于进程加锁,突然崩溃,未来得及解锁,导致其他进程访问不了...
  2. 蜻蜓FM涉嫌诈骗投资人和广告主源代码剖析
  3. LevelDB 源码剖析(三)公共基础:内存管理、数值编码、Env家族、文件操作
  4. 机器学习——支持向量机SVMpython实现
  5. 免费数据集获取加速器|Graviti Open Datasets
  6. 在DevStack中使用Systemd
  7. 指定的網域的名稱或安全性識別碼(用磁碟映像檔部署的電腦無法加入AD網域 )...
  8. java form上传图片_js formData图片上传(单图上传、多图上传)后台java
  9. java 拦截器实现接口调用频率限制
  10. 图书馆占座系统(SSM,JQUERY-EASYUI,MYSQL)
  11. 《华为工作法》学习笔记
  12. 【移动安全基础篇】——26、两个简单app破解
  13. 《智慧工地单点解析系列(一)—— 劳务实名制》
  14. mysql默认数据库名_mysql默认数据库
  15. ibatis的isequal_ibatIS中的isNotNull、isEqual、isEmpty
  16. 云原生KubeSphere DevOps流水线部署RuoyiCloud
  17. python二进制写入文件_python读写二进制文件的方法
  18. 计算机等级考试四级 网络工程师 之 操作系统原理1 适合懒人备考哈哈哈
  19. RStudio安装xlsx包
  20. 近视眼学计算机好吗6,近视又不戴眼镜,还经常对着电脑会怎么样

热门文章

  1. 构造函数和析构函数深拷贝和浅拷贝
  2. BugkuCTF-Misc:多种方法解决
  3. JAVA去掉指定字符
  4. gateway集成sentinel实现网关限流
  5. div上下展开收缩 html,js实现div层缓慢收缩与展开的方法
  6. 【译】Attacks against machine learning — an overview
  7. All of Recurrent Neural Networks (RNN)
  8. ICLR 2017 | GAN Missing Modes 和 GAN
  9. Android 5.1 Lollipop的Zygote分析——上篇
  10. 在windows下将Tomcat设置为自动启动的服务