日月换新天。为有牺牲多壮志,敢教

[Swift]LeetCode458. 可怜的小猪 | Poor Pigs

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour.

Answer this question, and write an algorithm for the follow-up general case.

Follow-up:

If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison.


有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水。它们从外观看起来都一样。如果小猪喝了毒药,它会在15分钟内死去。

问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少只猪?

回答这个问题,并为下列的进阶问题编写一个通用算法。

进阶:

假设有 n 只水桶,猪饮水中毒后会在 m 分钟内死亡,你需要多少猪(x)就能在 p 分钟内找出“有毒”水桶?n只水桶里有且仅有一只有毒的桶。


 8ms

复制代码
 1 class Solution {
 2     func poorPigs(_ buckets: Int, _ minutesToDie: Int, _ minutesToTest: Int) -> Int {
 3         if buckets == 1 {return 0}
 4         //每头小猪最多可测试的水桶数
 5         var  times = minutesToTest / minutesToDie + 1    
 6         var num:Int = 0
 7         //假设5桶水,一头小猪一个小时内检测四个桶
 8         //如果没死,则是最后一桶有毒。
 9         //每增加一头小猪,能检测的桶数乘5
10         while(pow(Double(times),Double(num)) < Double(buckets))
11         {           
12             num += 1
13         }
14         return num
15     }
16 }
复制代码

8ms

1 class Solution {
2     func poorPigs(_ buckets: Int, _ minutesToDie: Int, _ minutesToTest: Int) -> Int {
3         let a: Double = log(Double(minutesToTest) / Double(minutesToDie) + 1)
4         let pigs: Double = log(Double(buckets)) / a
5         return Int(pigs.rounded(.up))
6     }
7 }

8ms

复制代码
1 class Solution {
2     func poorPigs(_ buckets: Int, _ minutesToDie: Int, _ minutesToTest: Int) -> Int {
3         var pigs = 0.0
4         while pow(Double(minutesToTest / minutesToDie + 1), pigs) < Double(buckets) {
5             pigs += 1
6         }
7         return Int(pigs)
8     }
9 }
复制代码

12ms

复制代码
 1 class Solution {
 2     func poorPigs(_ buckets: Int, _ minutesToDie: Int, _ minutesToTest: Int) -> Int {
 3         var pigs = 0
 4         while Int(pow(Double((minutesToTest / minutesToDie) + 1), Double(pigs))) < buckets {
 5             pigs += 1
 6         }
 7         
 8         return pigs
 9     }
10 }
复制代码

 

posted @   为敢技术  阅读(281)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示
哥伦布
13:14发布
哥伦布
13:14发布
3°
西南风
2级
空气质量
相对湿度
95%
今天
中雨
3°/13°
周三
大雨
1°/13°
周四
中雨
3°/8°