poor-pigs(非常好的思路)
https://leetcode.com/problems/poor-pigs/
package com.company;
class Solution {
// 下面第二种做法貌似是OJ原来的做法,但是是错误的
// 看了这个解答 https://discuss.leetcode.com/topic/66856/major-flaw-in-current-algorithm-fixed
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
// 有5种状态
int circle = minutesToTest / minutesToDie + ;
int ret = ;
long num = ;
while (num < buckets) {
num *= circle;
ret++;
}
return ret;
}
// 以下答案不太对
public int poorPigs2(int buckets, int minutesToDie, int minutesToTest) {
if (minutesToDie == ) {
return ;
}
int circle = minutesToTest / minutesToDie;
if (circle == ) {
return ;
}
int batch = (buckets + (circle - )) / circle;
int ret = ;
long num = ;
while (num < batch) {
num *= ;
ret++;
}
if (num == batch && circle != ) {
return ret + ;
}
else {
return ret;
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello!");
Solution solution = new Solution();
// Your Codec object will be instantiated and called as such:
int ret = solution.poorPigs(, , );
System.out.printf("ret:%d\n", ret);
System.out.println();
}
}
poor-pigs(非常好的思路)的更多相关文章
- 1228.Poor Pigs 可怜的猪
转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...
- 【LeetCode】458. Poor Pigs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode: Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- [LeetCode] Poor Pigs 可怜的猪
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- [Swift]LeetCode458. 可怜的小猪 | Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- LeetCode算法题-Poor Pigs(Java实现)
这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...
- [LeetCode&Python] Problem 458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- Leetcode458.Poor Pigs可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
随机推荐
- linux环境搭建系列之svn安装
前提: linux centOS 64位操作系统 1.root账号 2.#yum install -y subversion 出现如下报错: 尝试Telnet https://www.baidu.co ...
- c语言入门-01
当我们学c语言我们学些什么. [1]编译机制 当我们写好c的代码,生产了程序,这中间到底做了些什么? 这个就是c语言的编译过程 我们分别来解析这上面的过程. 我们写出我们第一个c程序. #includ ...
- TopK-微博今日热门话题
大纲 TopK on single node TopK on multiple nodes Realtime topK with low QPS Realtime topK with high QPS ...
- springMvc的400问题
主要是参数类型对不上导致的 本文主要记录一些作者在使用spring mvc过程中遇到的一些以及解决办法,以备日后查询或者供其他网友阅读,每个问题的解决办法肯定不止一种,如果你也遇到过类似问题,并且有独 ...
- FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令
FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令 来源 https://www.liurongxing.com/freebsd-tips.html 来源 http://blog.51c ...
- [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划
[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划 试题描述 一个餐厅在相继的 \(N\) 天里,第 \(i\) 天需要 \(R_i\) 块餐巾 \((i=l,2,-,N)\) ...
- linux系统——etc下的profile文件
/etc/profile文件 /etc/profile是全局的,适用于所有的shell.在刚登录Linux时,首先启动 /etc/profile 文件. profile文件会告诉shell使用什么语言 ...
- Bridges
Bridges 题目描述 YYD为了减肥,他来到了瘦海,这是一个巨大的海,海中有n个小岛,小岛之间有m座桥连接,两个小岛之间不会有两座桥,并且从一个小岛可以到另外任意一个小岛.现在YYD想骑单车从小岛 ...
- 【VBA】标准Sub/Function定义,带ScreenUpdating、On Error GoTo
[说明] 标准Sub/Function定义,带ScreenUpdating.On Error GoTo Sub AutoFillRole() '--------------- ERROR MSG--- ...
- 【02】webpack 之 入门
http://www.jianshu.com/p/42e11515c10f 写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可 ...