Skip to content

Commit 588158f

Browse files
committed
solve problem Poor Pigs
1 parent ca32dcc commit 588158f

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ All solutions will be accepted!
216216
|706|[Design Hashmap](https://leetcode-cn.com/problems/design-hashmap/description/)|[java/py/js](./algorithms/DesignHashmap)|Easy|
217217
|872|[Leaf Similar Trees](https://leetcode-cn.com/problems/leaf-similar-trees/description/)|[java/py/js](./algorithms/LeafSimilarTrees)|Easy|
218218
|427|[Construct Quad Tree](https://leetcode-cn.com/problems/construct-quad-tree/description/)|[java/py](./algorithms/ConstructQuadTree)|Easy|
219+
|458|[Poor Pigs](https://leetcode-cn.com/problems/poor-pigs/description/)|[java/py/js](./algorithms/PoorPigs)|Easy|
219220

220221
# Database
221222
|#|Title|Solution|Difficulty|

algorithms/PoorPigs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Poor Pigs
2+
We can solve this problem by bitwise or log function

algorithms/PoorPigs/Solution.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
3+
return (int) Math.ceil(Math.log(buckets) / Math.log(minutesToTest / minutesToDie + 1));
4+
}
5+
}

algorithms/PoorPigs/solution.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {number} buckets
3+
* @param {number} minutesToDie
4+
* @param {number} minutesToTest
5+
* @return {number}
6+
*/
7+
var poorPigs = function(buckets, minutesToDie, minutesToTest) {
8+
let base = parseInt(minutesToTest / minutesToDie) + 1
9+
return Math.ceil(Math.log(buckets) / Math.log(base))
10+
};

algorithms/PoorPigs/solution.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def poorPigs(self, buckets, minutesToDie, minutesToTest):
3+
"""
4+
:type buckets: int
5+
:type minutesToDie: int
6+
:type minutesToTest: int
7+
:rtype: int
8+
"""
9+
base = minutesToTest / minutesToDie + 1
10+
buckets -= 1
11+
count = 0
12+
while buckets > 0:
13+
buckets /= base
14+
count += 1
15+
return count

0 commit comments

Comments
 (0)