Java实现 LeetCode 458 可怜的小猪
458. 可怜的小猪
有 1000 只水桶,其中有且只有一桶装的含有毒药,其余装的都是水。它们从外观看起来都一样。如果小猪喝了毒药,它会在 15 分钟内死去。
问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少只猪?
回答这个问题,并为下列的进阶问题编写一个通用算法。
进阶:
假设有 n 只水桶,猪饮水中毒后会在 m 分钟内死亡,你需要多少猪(x)就能在 p 分钟内找出 “有毒” 水桶?这 n 只水桶里有且仅有一只有毒的桶。
提示:
可以允许小猪同时饮用任意数量的桶中的水,并且该过程不需要时间。
小猪喝完水后,必须有 m 分钟的冷却时间。在这段时间里,只允许观察,而不允许继续饮水。
任何给定的桶都可以无限次采样(无限数量的猪)。
PS:
如果 minutesToTest / minutesToDie = 0,那么每一只猪只有一种状态,即存活。
如果 minutesToTest / minutesToDie = 1,那么每一只猪有两种状态,存活或者死亡。
进一步而言,如果 minutesToTest / minutesToDie = 2,那么每一只猪有三种状态,存活、在第一次测试后死亡、在第二次测试后死亡。 因此每一只猪的状态数量为 states = minutesToTest / minutesToDie + 1。
1 只猪可以测试 1 个水桶
2 只猪可以测试 4 个水桶。
x 只猪可以测试 2^x 个水桶。

class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int states = minutesToTest / minutesToDie + 1;
return (int) Math.ceil(Math.log(buckets) / Math.log(states));
}
}
Java实现 LeetCode 458 可怜的小猪的更多相关文章
- LeetCode 每日一题 458. 可怜的小猪
题目描述 有 buckets 桶液体,其中 正好 有一桶含有毒药,其余装的都是水.它们从外观看起来都一样.为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断.不幸的是,你只有 m ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- jmeter录制rabbitmq消息-性能测试
一.目的 为了测试系统的稳定性,在UAT环境下,通一段时间内不间断发送MQ消息来验证系统是否会出现异常. 二.测试工具 使用测试工具:jmeter5.2.1,火狐浏览器71.0,RabbitMQ管理 ...
- Git、Github习笔记01——Git本地仓库
作者:Eventi 出处:http://www.cnblogs.com/Eventi 欢迎转载,也请保留这段声明.谢谢! git简介 版本控制软件,由Linus(linux开发者)开发,最初用来对li ...
- 基于 abp vNext 和 .NET Core 开发博客项目
项目介绍 此个人博客项目底层基于 ABP Framework (不完全依赖)搭建项目 和免费开源跨平台的 .NET Core 3.1 开发,可作为 .NET Core 入门项目进行学习,支持各种主流数 ...
- Springboot 上传CSV文件并将数据存入数据库
.xml文件依赖配置 <!--csv依赖 --> <dependency> <groupId>org.apache.commons</groupId> ...
- React:Lifting State Up
在学习React的组件的时候,我也好奇组件间需要共享状态或通信的时候,React是如何处理的.在文档的QUICK START的提到Lifting State Up(状态提升),并不是什么新鲜东西.只是 ...
- MySQL(9)— 规范数据库设计
九.规范数据库设计 9-1.为什么要设计? 当数据库比较复杂时,我们就需要设计了! 糟糕的数据库设计: 数据冗余,浪费大量存储空间 使用物理外键,大量的增删改操作麻烦,异常 查询效率低下 良好的数据库 ...
- PAT-1060 Are They Equal (科学计数法)
1060. Are They Equal If a machine can save only 3 significant digits, the float numbers 12300 and 1 ...
- Poj 最短路和次短路的个数 Dij+优化?。
Description Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every ...
- ShoneSharp语言(S#)的设计和使用介绍系列(7)— 布尔Bool及对象Object
ShoneSharp语言(S#)的设计和使用介绍 系列(7)— 布尔Bool及对象Object 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ ...
- swiper插件在移动端,多个swiper左右滑动时有空白的问题
之前在项目上用到了多个swiper.但是结构结构代码css.以及js 几乎一样的除了第一个swiper左右滑动有回弹.其他都没有回弹.于是尝试了各种方法都不行. 百思不得其解 ,最后在官网终于找到了 ...