-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoorPigs.py
25 lines (21 loc) · 1.03 KB
/
poorPigs.py
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
'''
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.
'''
"""
:type buckets: int
:type minutesToDie: int
:type minutesToTest: int
:rtype: int
"""
import math
class Solution(object):
def poorPigs(self, buckets, minutesToDie, minutesToTest):
return int(math.ceil(math.log(buckets)/math.log(minutesToTest/minutesToDie+1)))
if __name__ == "__main__":
sol = Solution()
buckets = 1000
minutesToTest , minutesToDie = 60 , 15
print(sol.poorPigs(buckets, minutesToDie, minutesToTest))