mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «875. Koko Eating Bananas»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
3a5d2ec0e6
commit
9bce3c655a
1 changed files with 22 additions and 0 deletions
22
cs/koko-eating-bananas.cs
Normal file
22
cs/koko-eating-bananas.cs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
public class Solution {
|
||||||
|
public int MinEatingSpeed(int[] piles, int h) {
|
||||||
|
double getTotal(int hourly) => piles.Sum(p => double.Ceiling(p / (double)hourly));
|
||||||
|
|
||||||
|
int answer = -1;
|
||||||
|
|
||||||
|
int low = 1, high = piles.Max();
|
||||||
|
while (low <= high) {
|
||||||
|
var mid = (low + high) / 2;
|
||||||
|
var total = getTotal(mid);
|
||||||
|
|
||||||
|
if (total <= h) {
|
||||||
|
answer = mid;
|
||||||
|
high = mid - 1;
|
||||||
|
} else {
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue