Skip to content

Commit a9eb0ee

Browse files
committedMar 1, 2025
Add solution #458
1 parent 9f02777 commit a9eb0ee

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@
366366
455|[Assign Cookies](./0455-assign-cookies.js)|Easy|
367367
456|[132 Pattern](./0456-132-pattern.js)|Medium|
368368
457|[Circular Array Loop](./0457-circular-array-loop.js)|Medium|
369+
458|[Poor Pigs](./0458-poor-pigs.js)|Hard|
369370
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
370371
461|[Hamming Distance](./0461-hamming-distance.js)|Easy|
371372
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|

‎solutions/0458-poor-pigs.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

Comments
 (0)
Please sign in to comment.