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 可怜的小猪的更多相关文章

  1. LeetCode 每日一题 458. 可怜的小猪

    题目描述 有 buckets 桶液体,其中 正好 有一桶含有毒药,其余装的都是水.它们从外观看起来都一样.为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断.不幸的是,你只有 m ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Day_12【集合】扩展案例1_利用集合的知识对长度为10的int数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序

    分析以下需求,并用代码实现 1.定义一个长度为10的int数组,并存入10个int类型的数据,其中有一些数据是重复的 2.利用集合的知识对数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序 3 ...

  2. QTableWidget自定义委托

    QTableWidget单元格使用自定义的lineEdit控件导致不能选中 使用自定义委托解决 1.自定义委托 class LineEditDelegate : public QItemDelegat ...

  3. 使用 React hooks 转化 class 的一些思考

    Hooks 是 React 16.8 的新增特性.它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性. 使用 React hooks 转化 class 的一些思考 ...

  4. 3、get请求(url详解)

    前言 上一篇介绍了Composer的功能,可以模拟get和post请求,get请求有些是不带参数的,这种比较容易,直接放到url地址栏就行.有些get请求会带有参数,本篇详细介绍url地址格式. 一. ...

  5. 案例 (一)如何把python项目部署到linux服务器上

      一.背景 用Python写了个脚本,需要部署到Linux环境的服务器上,由于服务器linux系统(centos,redhat等)自带的是python2,现在的python萌新都是从python3开 ...

  6. P1251 餐巾计划问题 网络流

    P1251 餐巾计划问题 #include <bits/stdc++.h> using namespace std; typedef long long ll; , inf = 0x3f3 ...

  7. Django操作session实例

    session项目文件: templates模板: login.html {% load static %} <!DOCTYPE html> <html lang="en& ...

  8. jquery 根据 option 的 text 定位选中 option

    $('#test option[text="b"]').attr("selected",true); 上面的方法在 jquery 低于 1.4.2 的版本(含) ...

  9. Spring Bean 后置处理器

    Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等. 你也可以在 ...

  10. LightOJ1282

    题目大意: 给出 n 和 k,请你求出 n^k 次方的前三位和后三位. 解题思路: 后三位用快速幂,不加赘述. 求前三位的方法: AC代码: #include <iostream> #in ...