LeetCode 458. Poor Pigs

  • 问题描述

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

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

  • 解决方案

类似于键盘扫描确定哪个按键被按下的思想。代码如下:

public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
       int base = minutesToTest / minutesToDie + 1;
       double res = Math.log(buckets) / Math.log(base);
        return (int)Math.ceil(res);
    }
展开阅读全文
©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值