首页 > 代码库 > poor-pigs(非常好的思路)
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(); } }
poor-pigs(非常好的思路)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。