Airbnb | OA 2019 | Halloween Candy
7513


Related problems:

Java solution

Time complexity: O(p * logMAX), where p is the number of piles and MAX is the largest pile.
Space complexity: O(1).

public static int solution(int[] candyPiles, int numHours) {
    int lo = 1;
    int hi = IntStream.of(candyPiles).max().orElse(lo);
    while (lo < hi) {
        int mid = (lo + hi) >>> 1;
        if (requiredHours(candyPiles, mid) > numHours) {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    return lo;
}

private static int requiredHours(int[] candyPiles, int c) {
    int hours = 0;
    for (int pile : candyPiles) {
        hours += (int) Math.ceil((double) pile / c);
    }
    return hours;
}
Comments (8)