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 ...
随机推荐
- Linux内核驱动学习(六)GPIO之概览
文章目录 前言 功能 如何使用 设备树 API 总结 前言 GPIO(General Purpose Input/Output)通用输入/输出接口,是十分灵活软件可编程的接口,功能强大,十分常用,SO ...
- 设计者模式之GOF23命令模式
命令模式Command 将一个请求封装为一个对象,从而使我们可用不同的请求对客户参数化:对请求排队或者记录请求日志,以及支持可撤销的操作.也称之为:动作Action模式,事务transaction模式 ...
- mybatis 自定义缓存 cache
缓存不管哪个框架都是显得特别的重要,今天自己测试实现了mybatis自定义缓存,从而理解mybatis缓存的工作原理. 首先缓存类要实现Cache接口:具体实现如下package com.ibatis ...
- 计算python内部数据结构时间效率-源代码
#Author:qinjiaxi '''本程序计算各种循环的时间效率''' from timeit import Timer def test1(n): li = [] for i in range( ...
- spark机器学习从0到1特征抽取–Word2Vec(十四)
一.概念 Word2vec是一个Estimator,它采用一系列代表文档的词语来训练word2vecmodel.该模型将每个词语映射到一个固定大小的向量.word2vecmodel使用文档中每个词 ...
- 微信小程序上传文件时弹出当前系统代理不是安全代理,是否信任
我的开发环境是.net core 启用了https,而微信的开发者工具不认这个证书. 解决办法1:关闭https 然后在 Startup.cs 中关闭注释掉 app.UseHttpsRedirecti ...
- Element Form表单实践(上)
作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...
- EMAIL、用户名测试点
EMAIL xxxaa@xxx.xxx 1.没有@情况,如:aa.net 2.没有.符号,如:aa@qqcom 3..后面没有字符:如 xxxaa@xxx. 4..不在@后面, 如:xxxaa.@xx ...
- python3.x 基础一:dict字典
字典,{key,value} help(dict) 定义一个字典: >>> dict1 {', 'name': 'yzw'} >>> dict2=dict1 > ...
- 【Java8新特性】关于Java8的Stream API,看这一篇就够了!!
写在前面 Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一个则是 Stream API(java.util.stream.*) ,那什么是Stream API呢?Java8中的 ...