每日一练之poor pigs【leetcode No.458】——猪测毒问题

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.

分析:

本题可以这么考虑问题, 先是二维地排列罐子, 然后分别让两头猪去尝试找出哪一行和哪一列有毒.间隔时间为15分钟, 由于测试时间是60分钟 所以总共1只猪能测试5行或者5列. (这里不是4行或者4列, 因为如果前面4个测试猪都没死的话, 说明最后一行/最后一列肯定有毒). 总结一下,1个维度交给1只猪,  能帮我们检查出(测试时间/毒发时间 + 1)个维度单位.

那么回到二维的例子里面, 2只猪能帮我们检查5*5=25个罐子,那么如果是三维, 就是5^3 = 125个, 以此类推随着维度的上升,只要最后的水桶数大于我们需要检查的水桶数,就需要几头猪.

1   2   3   4   5

6   7   8   9  10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

所以 ,  pigs = ceil(log_5(1000)) = 5 pigs

代码比较简单主要是思路分析:

class Solution {
public:
    int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int tests=(minutesToTest/minutesToDie)+1;
        int pigs=0;
        while(pow(tests,pigs)<buckets)
       { pigs++;}
       return pigs;
    }
};


还有一种思路如下:

Eg. 4 buckets, 15 mins to die, and 15 mins to test.

The answer is 2. Suppose we use A and B to represent pigs, we could have

0_1478958363340_upload-5c96756b-ca9f-41f2-b597-ff57a040ebe8

Obviously we could use the binary form to represent the solution.

0_1478958322597_upload-0812d848-8375-4422-813e-b97bdd37c653

Conclusion: If we have x pigs, we could use them to represent (encode) 2^x buckets.

2. What if we could have more than one attempts?

Eg. 4 buckets, 15 mins to die, and 30 mins to test.

At the moment, I consider the problem as an encoding problem: With more attempts, how to use fewer pigs to represent all the buckets?

I got lost at this step by keep thinking the binary way. After hanging around the forum, I got the idea to change my views. Let's go back to the one shot situation. What does the binary form mean? It's much easier if we regard it as:

  • 0 means the pig does not drink and die.
  • 1 means the pig drinks in the first (and only) round.

We could generalise with:

  • 0 means the pig does not drink and die.
  • 1 means the pig drinks in the first round and die.
  • 2 means the pig drinks in the second round and die.
    ...
  • t means the pig drinks in the t-th round and die.

Conclusion: If we have t attempts, we could use t+1-based number to represent (encode) the buckets. (That's also why the first conclusion uses the 2-based number)

EXAMPLE

Eg. 8 buckets, 15 mins to die, and 40 mins to test.

We have 2 (= (40/15).floor) attempts, as a result we'll use 3-based number to encode the buckets.

How many pigs do we need? Answer is 2 (= Math.log(8, 3).ceil)

0_1478960257723_upload-0da5c126-bf43-4183-935d-c2c1a5b5df15

For example 3-based number 02 means: the pig A does not drink and die, and the pig B drinks in the second round and die.


参考资料:

https://discuss.leetcode.com/topic/67666/another-explanation-and-solution

https://discuss.leetcode.com/topic/67482/solution-with-detailed-explanation/2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值