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 + 1;
        int ret = 0;
        long num = 1;
        while (num < buckets) {
            num *= circle;
            ret++;
        }
        return ret;
    }
    // 以下答案不太对
    public int poorPigs2(int buckets, int minutesToDie, int minutesToTest) {
        if (minutesToDie == 0) {
            return 0;
        }
        int circle = minutesToTest / minutesToDie;
        if (circle == 0) {
            return 0;
        }
        int batch = (buckets + (circle - 1)) / circle;

        int ret = 0;
        long num = 1;
        while (num < batch) {
            num *= 2;
            ret++;
        }
        if (num == batch && circle != 1) {
            return ret + 1;
        }
        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(1000, 15, 60);
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}
时间: 2024-10-18 22:19:23

poor-pigs(非常好的思路)的相关文章

[LeetCode] Poor Pigs

There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucke

Leetcode - 458 Poor Pigs

题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? 分析: 为什么可怜不言而喻...本题可以这么考虑问题, 先是二维地排列罐子, 然后分别让两头猪去尝试找出哪一行和哪一列有毒.间隔时间为15分钟, 由于测试时间是60分钟 所以总共1只猪能测试5行或者5列. (这里不是4行或者4列, 因为如果前面4个测试猪都没死的话, 说明最后一行/最后一列肯定有毒

[LeetCode&amp;Python] Problem 458. Poor Pigs

There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucke

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

PIGS(最大流)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18742   Accepted: 8511 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

POJ 1149 PIGS(最大流)

POJ 1149 PIGS 题目链接 题意:有n个猪圈,m个顾客,猪圈中一开始有一些猪,顾客轮流来(注意是有先后顺序的),然后每个顾客会开启一些猪圈,在开启的猪圈中最多买b只猪,之后可以任意把剩下的猪分配到开着的猪圈中,问最多能卖出几只猪 思路:这题的关键在于建模,由于顾客有先后顺序,假如后来的顾客会开启x门,前面一个顾客也会开启x门,那么前面顾客相当与可以分配给后面顾客, 所以建模的方式为,源点和每个猪圈连,容量为猪圈猪数,每个猪圈和第一个开的顾客连,如果后面有顾客会开这个猪圈,则和之前的顾客

HDU 4956 Poor Hanamichi

HDU 4956 Poor Hanamichi 题目链接 思路:直接从l往上找判断即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; int t; ll l, r; bool judge(ll num) { ll flag = 1; ll ans = 0; whi

HDU 4952 Poor Mitsui(贪心)

HDU 4957 Poor Mitsui 题目链接 思路:利用相邻交换法去贪心即可,注意容积为0的情况,这是个坑点 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 45; struct SB { int a, b; } sb[N]; bool cmp(SB x, SB y) { return x.b * y.a < x.

BNUOJ 34025 -Poor Warehouse Keeper(贪心)

题目:BNUOJ 34025 -Poor Warehouse Keeper(贪心) 题目大意:有一个商品的信息表,上面是数量,下面是总价,然后旁边各有一个按钮.上面的数量按钮按一下数量就加1,然后价格对应的也要在加上一个当前的单价.下面的按钮按一下的话,就对应的总价加1.初始状态是 1 1,然后给出终点状态,问能否得到.可以的话输出最少要按几次按钮,否则输出-1:总价每次输出都是下取整. 解题思路:如果想要尽快的得到总点状态的值,那么就应该按总价的按钮,因为只有总价变大了,商品对因的单价就上升了

POJ 1149 PIGS(Dinic最大流)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t