Skip to content

Commit 63e4d70

Browse files
committed
458_Poor_Pigs
1 parent 069f4f5 commit 63e4d70

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Also, there are open source implementations for basic data structs and algorithm
145145
| 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/443_String_Compression.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/443_String_Compression.java) | Maintain curr, read, write and anchor (start of this char). |
146146
| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/448_Find_All_Numbers_Disappeared_in_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/448_Find_All_Numbers_Disappeared_in_an_Array.java) | Value (1, n) and index (0, n-1). Mark every value postion as negative. Then, the remain index with positive values are result. O(n)|
147147
| 453 | [Number of Segments in a String](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/453_Minimum_Moves_to_Equal_Array_Elements.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/453_Minimum_Moves_to_Equal_Array_Elements.java) | Each move is equal to minus one element in array, so the answer is the sum of all elements after minus min. |
148+
| 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/458_Poor_Pigs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/458_Poor_Pigs.java) | [2 pigs for 5 * 5 metric](https://leetcode.com/problems/poor-pigs/discuss/94266/Another-explanation-and-solution) |
148149
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/461_Hamming_Distance.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/461_Hamming_Distance.java) | Hamming Distance is related to XOR for numbers. So, XOR then count 1. O(n) |
149150
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit |
150151
| 475 | [Heaters](https://leetcode.com/problems/heaters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/475_Heaters.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/475_Heaters.java) | 1. Binary search hourse in heater array, O(nlogn) and O(1)<br> 2. Two points, O(nlogn) and O(1) |

java/458_Poor_Pigs.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
3+
int n = minutesToTest / minutesToDie + 1;
4+
int pigs = 0;
5+
while (Math.pow(n, pigs) < buckets) pigs++;
6+
return pigs;
7+
}
8+
}

python/458_Poor_Pigs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def poorPigs(self, buckets, minutesToDie, minutesToTest):
3+
"""
4+
:type buckets: int
5+
:type minutesToDie: int
6+
:type minutesToTest: int
7+
:rtype: int
8+
"""
9+
# https://leetcode.com/problems/poor-pigs/discuss/94266/Another-explanation-and-solution
10+
pigs = 0
11+
while (minutesToTest / minutesToDie + 1) ** pigs < buckets:
12+
pigs += 1
13+
return pigs

0 commit comments

Comments
 (0)