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(非常好的思路)的更多相关文章

  1. 1228.Poor Pigs 可怜的猪

    转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...

  2. 【LeetCode】458. Poor Pigs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. Leetcode: Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  4. Leetcode - 458 Poor Pigs

    题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...

  5. [LeetCode] Poor Pigs 可怜的猪

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  6. [Swift]LeetCode458. 可怜的小猪 | Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  7. LeetCode算法题-Poor Pigs(Java实现)

    这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...

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

  9. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  10. Leetcode458.Poor Pigs可怜的小猪

    有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...

随机推荐

  1. scp -v 查看具体的过程

    前几天跟同事讨论scp 多个文件和 scp多个文件夹的压缩包那个快. 老大说,压缩包快,压缩包传输可以避免每个文件的重建连接,不过文件系统的遍历.目录创建.检验会有一些开销. 他建议我scp -v看下 ...

  2. iOS开发UI篇—CALayer

      一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全 ...

  3. cf 843 D Dynamic Shortest Path [最短路+bfs]

    题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...

  4. 二进制包部署Kubernetes集群

    今天这篇文章教给大家如何快速部署一套Kubernetes集群.K8S集群部署有几种方式:kubeadm.minikube和二进制包.前两者属于自动部署,简化部署操作,我们这里强烈推荐初学者使用二进制包 ...

  5. GROUP BY子句的查询中显示COUNT()为0的结果

    含有GROUP BY子句的查询中如何显示COUNT()为0的结果 在SQL Server数据库查询中,为了对查询结果进行对比.分析,我们经常会用到GROUP BY子句以及COUNT()函数来对查询结果 ...

  6. 关于设置组件的state时遇到的一些问题

    在使用react-bootstrap的时候设置showModel的值来控制Model的显示与隐藏,但是setState这个函数是异步的. 当你进行数据更新的时候,接着执行函数获取这个模态框的dom是获 ...

  7. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

  8. 【CF1043B】Lost Array(枚举)

    题意:给定n与数组a,求所有的k使得存在x数组能按以下规则构造出a n<=1e3,a[i]<=1e6 思路: #include<cstdio> #include<cstr ...

  9. C# Log日志工具类

    using System; using System.Collections.Generic; using System.Text; using System.IO; public class Log ...

  10. Codeforces 935D Fafa and Ancient Alphabet

    题目链接 题意 给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率. 思路 从高位到低位,根据每一位上相应的\( ...