leetcode 458. Poor Pigs
来源:互联网 发布:linux怎么编辑文档 编辑:程序博客网 时间:2024/06/05 07:30
原题:
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.
参考解析的连接:https://www.cnblogs.com/mux1/p/6275797.html
首先感谢楼上的博主给的答案。
我第一反应是做二进制,但是这样就算是几分的话,也不是最少的。
毕竟用维度这种能缩到最小,也是学到了。
代码很简单,不过照例还是贴一下:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) { int temp=minutesToTest/minutesToDie+1; int amount=1; int result=0; while(amount<buckets) { result++; amount*=temp; } return result;}
- LeetCode 458. Poor Pigs
- 【leetcode】458. Poor Pigs
- Leetcode 458. Poor Pigs
- [Leetcode] 458. Poor Pigs
- leetcode 458. Poor Pigs
- LeetCode 458.Poor Pigs
- LeetCode 458.poor pigs
- leetcode 458. Poor Pigs
- leetcode 458. Poor Pigs
- leetcode- Add to List 458. Poor Pigs
- [LeetCode]Poor Pigs
- [LeetCode] Poor Pigs
- 458. Poor Pigs
- 458. Poor Pigs
- 458. Poor Pigs
- 458. poor pigs
- 458. Poor Pigs
- 每日一练之poor pigs【leetcode No.458】——猪测毒问题
- 分享一下简单的 canvas 翻角效果
- 状态压缩
- QRCode生成二维码并带有logo图片
- 阿里云服务器MySQL(5.7V)数据库用Navicat连接
- 瑞芯微开发编译过程参考
- leetcode 458. Poor Pigs
- 一些好文章的地址
- MapReduce之Shuffle
- win7下安装docker
- 微信小程序缓存-------缓存时效性
- 个人遇到的自定义dialog中内容无法居中的问题
- mysql数据导入的三种方法
- Orcal 中的树级列表 Start with connect by prior 递归查询
- phpstorm 结合 YUI Compressor 实现自动压缩文件