875. Koko Eating Bananas
Binary searchT:O(n + plogp) - p is piles value, n is piles length
S:O(1)```java
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int left = 1;
int right = 0;
for (int pile : piles) {
right = Math.max(right, pile);
}
while (left < right) {
int mid = left + (right - left)/2;
int count = 0;
for (int pile : piles) {
if (mid == 0) {
return left;
} else {
count += pile/mid + (pile%mid != 0 ? 1 : 0);
}
}
if (count <= h) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
/*
Binary search
T:O(n + plogp) - p is piles value, n is piles length
S:O(1)
piles = [3,6,7,11], h = 8
h = 8
3, 6, 7, 11
1 2. 2. 3
32
piles[i]/k + (piles[i]%k != 0 ? 1 : 0)
*/
```new version
Last updated
Was this helpful?