leetcode--458--, poor little piggy.

Source: Internet
Author: User

Problem Description:

There are 1000 buckets, of which there is only one barrel containing toxic drugs, and the rest are water. They all look the same from the look. If the pig drank the poison, it would die in 15 minutes.

The question is, if you need in an hour to figure out which bucket contains poison, how many pigs do you need at least?

Answer this question and write a general algorithm for the following advanced questions.

Advanced:

Suppose there are n buckets, pigs will die in the water after poisoning, how many pigs (x) will you need to find a "poisonous" bucket in P minutes? N There is only one poisonous bucket in the bucket.

Method: Suppose 5 barrels of water, one hours a pig test four barrels, if not dead, it must be the last barrel poisonous. Number of barrels to be detected by 5,POW (5,num) > buckets for each additional end

1 classsolution (object):2     defpoorpigs (self, buckets, Minutestodie, minutestotest):3         """4 : Type Buckets:int5 : Type Minutestodie:int6 : Type Minutestotest:int7 : Rtype:int8         """9Times = Minutestotest/minutestodie + 1#maximum number of buckets that can be tested per pigTennum =0 One          whilePow (Times,num) <Buckets: Anum = num + 1 -         returnNum

2018-10-03 21:47:29

leetcode--458--, poor little piggy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.