Leetcode-458 Poor Pigs

Source: Internet
Author: User

Topic:

There are a total of 1000 jars, of which only 1 are poisons, and others are water. Now use a bunch of poor pigs to find the poison jar. The poison is known to give the pig the time of 15 minutes, so in 60 minutes, at least a few pigs to find the poison tank?

Analysis:

Why poor self-evident ... This can be considered as a matter of fact, first two-dimensional arrangement of the jar, and then let both pigs to try to find out which row and which column is poisonous. The interval is 15 minutes, because the test time is 60 minutes, so a total of 1 pigs can test 5 rows or 5 columns. (This is not a 4 row or 4 column, because if the previous 4 test pigs are not dead, the last line/last column must be poisonous). Summing up, 1 dimensions to 1 pigs, it dedicated heartily, can help us to check out (test time/toxicity time + 1) dimensions of units.

Then back to the two-dimensional example, 2 pigs can help us check 5*5=25 a jar, then if it is three-dimensional, is 5^3 = 125, and so on with the rise of the dimension, as long as the last bucket number than we need to check the number of buckets, you need a few pigs.

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

Code: (relatively simple)

public class solution{

public int Poorpigs (int buckets, int timetodie, int timetotest) {

int pigs = 0;

while (Math.pow ((Timetotest/timetodie + 1), pigs) < buckets) {

pig++;

}

return pigs;

}

}

Leetcode-458 Poor Pigs

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.