---
# System prepended metadata

title: 0458. Poor Pigs
tags: [Leetcode, Hard, Math]

---

# 0458. Poor Pigs
###### tags: `Leetcode` `Hard` `Math`
Link: https://leetcode.com/problems/poor-pigs/description/
## 思路
脑筋急转弯
思路[参考](https://leetcode.com/problems/poor-pigs/description/)
如果bucket数是```minutesToTest/minutesToDie+1```, 我们可以用一只pig就可以知道哪个bucket是posionous的
如果bucket数是```(minutesToTest/minutesToDie+1)^2```, 那么我们可以用两只pig 因为我们可以把```(minutesToTest/minutesToDie+1)^2```个bucket, 想成一个2D array, 边长是```minutesToTest/minutesToDie+1```, 一只pig检查row一只检查col即可
所以n只pig可以检查```(minutesToTest/minutesToDie+1)^n```个bucket
## Code
```java=
class Solution {
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int pigs = 0;
        while(Math.pow(minutesToTest/minutesToDie+1, pigs)<buckets){
            pigs += 1;
        }
        return pigs;
    }
}
```