zoukankan      html  css  js  c++  java
  • [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 bucket contains the poison within one hour.

    Answer this question, and write an algorithm for the follow-up general case.

    Follow-up:

    If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison.

    这是一个数学问题,给定n个水桶,其中只有1个含有毒药,假设猪喝了有毒的水会在m分钟后死亡,现在你有p分钟去实验,要求使用最少的猪x去检测出有毒药的桶。

    首先计算出做实验的次数t=p/m, 1只猪可以检测出t + 1个水桶中是否含有毒药。2只猪可以检测出(t + 1) ^ 2个水桶中是否含有毒药。则按照这个思路求解即可。

    class Solution {
    public:
        int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
            int pigs = 0;
            int times = minutesToTest / minutesToDie + 1;
            while (pow(times, pigs) < buckets)
                pigs++;
            return pigs;
        }
    };
    // 0 ms
  • 相关阅读:
    C#Socket发16进制以及进制转换
    WPF跨线程操作UI界面控件
    DispatcherTimer和Timer的区别
    C#等比列放大缩小图片
    计算进项税
    AX 中通过SqlServer数据库刷数据
    AX中日期的常用方法
    从AX2012系统批量生成CSV格式数据
    装箱单过发票
    导入CSV文件乱码
  • 原文地址:https://www.cnblogs.com/immjc/p/7270115.html
Copyright © 2011-2022 走看看