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.

这个题目很巧妙, 我们利用@StefanPochmannSolution和解释, 得到了通用的表达式, 也就是 (minutesToTest//minutesToDie + 1)**pigs >= buckets 中最小的pigs

note: 这里的edge case 是 minutesToTest >= minutesToDie

Code

class Solution(object):
def poorPigs(self, buckets, minutesToDie, minutesToTest):
"""
:type buckets: int
:type minutesToDie: int
:type minutesToTest: int
:rty
"""
      
     if minutesToTest < minutesToDie: return -1
pigs = 0
while (minutesToTest//minutesToDie +1)**pigs < buckets:
pigs += 1
return pigs

[LeetCode] 458. Poor Pigs_Easy tag: Math的更多相关文章

  1. Leetcode - 458 Poor Pigs

    题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...

  2. [LeetCode] 441. Arranging Coins_Easy tag: Math

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  3. [LeetCode] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  4. [LeetCode] 504. Base 7_Easy tag: Math

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

  5. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  7. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  8. [LeetCode] 492. Construct the Rectangle_Easy tag: Math

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

  9. 【LeetCode】458. Poor Pigs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 通过orderby关键字,LINQ可以实现升序和降序排序。LINQ还支持次要排序。

    通过orderby关键字,LINQ可以实现升序和降序排序.LINQ还支持次要排序. LINQ默认的排序是升序排序,如果你想使用降序排序,就要使用descending关键字. static void M ...

  2. C# 中对COOKIES的操作

    HttpUtility.UrlDecode HttpUtility.UrlEncode HttpContext.Current.Request.Cookies["UserCode" ...

  3. Android手机资料拷贝导出工具 --- 91手机助手

    http://zs.91.com/

  4. Linux命令 swap:内存交换空间

    swap 内存交换空间的概念 swap使用上的限制

  5. C语言位操作初步

    位操作允许程序员对单独的位进行操作,例如,早某些机器上,短整型占16位,位操作将每一位单独操作. 位操作允许程序员设置.清除.测试与其他的操作,这些操作如下表: 操作 含义 & 按位与 | 按 ...

  6. 【BZOJ2138】stone Hall定理+线段树

    [BZOJ2138]stone Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆包含Ai颗石子.每1分钟,Nan会 ...

  7. python selenium操作表格式元素实例

    很多时候,网页上的布局都是表格形式的,如出下面这样的 这种网页类型在自动化中比较头痛,需要很多判断,下面就举个例子,这里以深圳出入境网页为例,http://yysl.sz3e.com/wsyysq/s ...

  8. Pexpect学习:

    pexecpt run用法:格式:run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=Non ...

  9. Canvas裁剪Clip和Region、RegionIterator

    extends:http://blog.csdn.net/lonelyroamer/article/details/8349601 裁剪功能由Canvas提供的一系列的clip...方法 和quick ...

  10. GPU对数据的操作不可累加

    我想当然的认为GPU处理数据时可以共同访问内存,所以对数据的操作是累加的. 事实证明:虽然GPU多个核可以访问同一块内存,但彼此之间没有依赖关系,它们对这块内存的作用无法累加. 先看代码: #incl ...