forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_458.java
35 lines (30 loc) · 1.08 KB
/
_458.java
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
26
27
28
29
30
31
32
33
34
35
package com.fishercoder.solutions;
/**
* 458. Poor Pigs
*
* 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.
*/
public class _458 {
public static class Solution1 {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
if (buckets-- == 1) {
return 0;
}
int base = minutesToTest / minutesToDie + 1;
int count = 0;
while (buckets > 0) {
buckets /= base;
count++;
}
return count;
}
}
}