[LeetCode] 458. Poor Pigs_Easy tag: Math
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.
这个题目很巧妙, 我们利用@StefanPochmann的Solution和解释, 得到了通用的表达式, 也就是 (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的更多相关文章
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- [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 ...
- [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. ...
- [LeetCode] 504. Base 7_Easy tag: Math
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 【LeetCode】458. Poor Pigs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 通俗大白话来理解TCP协议的三次握手和四次分手
通俗理解: 但是为什么一定要进行三次握手来保证连接是双工的呢,一次不行么?两次不行么?我们举一个现实生活中两个人进行语言沟通的例子来模拟三次握手. 引用网上的一些通俗易懂的例子,虽然不太正确,后面会指 ...
- LinQ实战学习笔记(二) C#增强特性
C# 为支持LINQ添加了许多语言特性: 隐式类型局部变量 对象初始化器 Lambda表达式 扩展方法 匿名类型 了解这些新特性是全面了解LINQ的重要先解条件,因此请不要忽视它们. (一) 隐式类 ...
- Apache服务器SSL双向认证配置
以Win32版Apache与OpenSSL为例,介绍从创建数字证书到Apache配置的整个过程,希望对读者有所帮助. Apache是目前最流行的WEB服务器之一,借助OpenSSL库,我们可以在Apa ...
- android cannot locate symbol 'sigemptyset'问题解决
设备是android 4.1的平板电脑,支持armeabi-v7a和mips,为了能用上poco c++ lib,用cmake编译了poco mips架构的lib,但在android studio里引 ...
- linux指定某非root用户执行开机启动项的方法(gogs git)
以linux指定git用户在linux开机时执行启动gogs git为例: 以root登录linux 执行vi /etc/rc.d/rc.local 在文档末尾添加一行语句:su git -c &qu ...
- Windows Server 2012升级R2过程中意外关闭恢复原系统方法
2012升级R2过程中强制关闭了计算机,导致再次启动后蓝屏提示"BAD_SYSTEM_CONFIG_INFO".用2012安装盘进入尝试修复失败(安全模式什么的都不用想),进入命令 ...
- PHP服务器访问优化
常规的优化措施: 磁盘写入,网络安全,证书加密,CPU,内存,DNS解析,数据库优化,页面gzip压缩 PHP gzip压缩打开: 打开php目录下的php.ini文件,找到zlib.output_c ...
- Particle 粒子效果使用(适配微信小游戏,particle is not defined)
在微信小游戏中使用粒子效果 参考: 1. 粒子库下载地址 2. 粒子官方使用教程 3. 水友解决微信小游戏particle is not defined 一.下载第三方库 Git地址:https:// ...
- matlab中 数据保留有效位数
可以使用round函数 ,这函数原本功能是四舍五入 比如: >> A = 0.0326465;>> B = round(A*1000)/1000 B = 0.0330
- node项目部署相关问题
process.env process.env属性返回一个对象,包含了当前Shell的所有环境变量. 通常的做法是,新建一个环境变量NODE_ENV,用它确定当前所处的开发阶段,生产阶段设为produ ...