Thanks Leetcode for the platform. Got this question on Halloween candy
https://leetcode.com/discuss/interview-question/384262/airbnb-oa-2019-candy
Similar question:
https://leetcode.com/problems/koko-eating-bananas/
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
Solved using binary search.
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int max = Integer.MIN_VALUE;
for (int num: piles) {
max = Math.max(num,max);
}
if(h == piles.length){
return max;
}
int speed = Integer.MAX_VALUE;
int start = 0;
int end = max;
while (start <= end){
int mid = start + (end - start)/2;
if(isPossible(piles,mid,h) == true){
speed = mid;
end = mid - 1;
}else{
start = mid + 1;
}
}
return speed;
}
public static boolean isPossible(int[] piles, int speed ,int h){
int time = 0;
for (int i = 0; i < piles.length; i++) {
time += (int)Math.ceil(piles[i] * 1.0/speed);
}
return time <= h;
}
}Any improvements in this?