|
| 1 | +/** |
| 2 | + * 458. Poor Pigs |
| 3 | + * https://leetcode.com/problems/poor-pigs/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * There are buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure |
| 7 | + * out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether |
| 8 | + * they will die or not. Unfortunately, you only have minutesToTest minutes to determine which |
| 9 | + * bucket is poisonous. |
| 10 | + * |
| 11 | + * You can feed the pigs according to these steps: |
| 12 | + * 1. Choose some live pigs to feed. |
| 13 | + * 2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets |
| 14 | + * simultaneously and will take no time. Each pig can feed from any number of buckets, and each |
| 15 | + * bucket can be fed from by any number of pigs. |
| 16 | + * 3. Wait for minutesToDie minutes. You may not feed any other pigs during this time. |
| 17 | + * 4. After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket |
| 18 | + * will die, and all others will survive. |
| 19 | + * 5. Repeat this process until you run out of time. |
| 20 | + * |
| 21 | + * Given buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to |
| 22 | + * figure out which bucket is poisonous within the allotted time. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number} buckets |
| 27 | + * @param {number} minutesToDie |
| 28 | + * @param {number} minutesToTest |
| 29 | + * @return {number} |
| 30 | + */ |
| 31 | +var poorPigs = function(buckets, minutesToDie, minutesToTest) { |
| 32 | + const max = Math.floor(minutesToTest / minutesToDie) + 1; |
| 33 | + let result = 0; |
| 34 | + |
| 35 | + while (Math.pow(max, result) < buckets) { |
| 36 | + result++; |
| 37 | + } |
| 38 | + |
| 39 | + return result; |
| 40 | +}; |
0 commit comments