poor-pigs(非常好的思路)
2024-09-08 09:29:11
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(); } }
最新文章
- ES6笔记(3)-- 解构赋值
- Cordova - 使用Cordova开发iOS应用实战1(配置、开发第一个应用)
- WPS文字在表格中打字自动跳动
- nyoj 20
- Delphi 中的结构体与结构体指针
- 用户浏览器关闭cookie处理方法
- [C++程序设计]有关形参与实参,及返回值说明
- zabbix java api
- 385cc412a70eb9c6578a82ac58fce14c md5破解
- ArrayList循环遍历并删除元素的常见陷阱
- centos 7 Chrony 集群同步时间
- mysql实现多实例
- 【转】nvidia-smi 命令解读
- python测试开发django-52.xadmin添加自定义的javascript(get_media)
- 每天一个linux命令:top
- chmod语法
- 对数组的操作splice() 和slice() 用法和区别
- HMM模型学习笔记(前向算法实例)
- 查看php-fpm开启的进程数以及每个进程的内存限制
- 【Tomcat】部署Web到tomcat的四种方式
热门文章
- Spider_Man_6 の Scrapy(未完待续)
- Halcon17 Linux 下载
- [python][django学习篇][4]django完成数据库代码翻译:迁移数据库(migration)
- 2017年浙江工业大学之江学院程序设计竞赛决赛 I: qwb VS 去污棒(可持久化Trie+离线)
- centos安装arm交叉工具链后常见的问题解决
- Sabota?
- AtCoder Regular Contest 075 C D E (暂时)
- 转 网络编程学习笔记一:Socket编程
- 对/proc和/sys的一些理解
- SaltStack 模块学习之拷贝master服务器上文件和目录到minion服务器