Skip to content

Commit 7d8b738

Browse files
[LEET-458] add 458
1 parent 1ae9ff6 commit 7d8b738

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
|462|[Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumMovestoEqualArrayElementsII.java) | O(nlogn) |O(1) | Medium|
3030
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/HammingDistance.java) | O(n) |O(1) | Easy|
3131
|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RepeatedSubstringPattern.java)| O(n)|O(n) | Easy| KMP
32+
|458|[Poor Pigs](https://leetcode.com/problems/poor-pigs/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PoorPigs.java) | O(1) |O(1) | Easy| Math
3233
|456|[132 Pattern](https://leetcode.com/problems/132-pattern/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_132Pattern.java) | O(n) |O(n) | Medium| Stack
3334
|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/AssignCookies.java)| O(n)|O(1) | Easy|
3435
|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_4SumII.java) | O(n) |O(n) | Medium| HashMap
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* 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.
5+
6+
Answer this question, and write an algorithm for the follow-up general case.
7+
8+
Follow-up:
9+
10+
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.
11+
*/
12+
public class PoorPigs {
13+
14+
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
15+
if (buckets--==1){
16+
return 0;
17+
}
18+
int base=minutesToTest/minutesToDie+1;
19+
int count=0;
20+
while (buckets>0){
21+
buckets/=base;
22+
count++;
23+
}
24+
return count;
25+
}
26+
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.PoorPigs;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class PoorPigsTest {
11+
private static PoorPigs test;
12+
private static int expected;
13+
private static int actual;
14+
private static int buckets;
15+
private static int minutesToDie;
16+
private static int minutesToTest;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new PoorPigs();
21+
}
22+
23+
@Before
24+
public void setupForEachTest(){
25+
expected = 0;
26+
actual = 0;
27+
}
28+
29+
@Test
30+
public void test1(){
31+
buckets = 1000;
32+
minutesToDie = 15;
33+
minutesToTest = 60;
34+
expected = 5;
35+
actual = test.poorPigs(buckets, minutesToDie, minutesToTest);
36+
assertEquals(expected, actual);
37+
}
38+
39+
@Test
40+
public void test2(){
41+
buckets = 1;
42+
minutesToDie = 1;
43+
minutesToTest = 1;
44+
expected = 0;
45+
actual = test.poorPigs(buckets, minutesToDie, minutesToTest);
46+
assertEquals(expected, actual);
47+
}
48+
49+
@Test
50+
public void test3(){
51+
buckets = 1000;
52+
minutesToDie = 12;
53+
minutesToTest = 60;
54+
expected = 4;
55+
actual = test.poorPigs(buckets, minutesToDie, minutesToTest);
56+
assertEquals(expected, actual);
57+
}
58+
}

0 commit comments

Comments
 (0)