Medium dsa binary-search

Minimum Time Required

The 30 second answer

Each machine produces one item every machines[i] days, all running at once, and I need the fewest days to produce a given goal. I binary-search the number of days. For a candidate day count I add days / machines[i] for each machine, stopping early if the total already meets the goal, and I search for the first day count that is enough.

Could you say this in an interview?

Problem You have machines that all run at the same time. Machine i produces one item every machines[i] days. Return the minimum number of days needed to produce goal items. Original: HackerRank: Minimum Time Required.

Understanding the prompt Production on day d is the sum of floor(d / machines[i]). That sum is non-decreasing in d, so the first d that reaches the goal is the answer. The value of d can be as large as the slowest machine times the goal, so the bounds and the production total belong in 64-bit integers.

Pattern recognition Binary search on the answer, with a monotonic production check. It is the same template as Koko Eating Bananas, this time on days rather than speed.

Brute force Increment the day count until production reaches the goal, which is too slow for the official limits.

Optimal lo = 1, hi = max(machines) * goal, both longs. While lo < hi, mid = lo + (hi - lo) / 2. If the machines produce at least goal items in mid days, hi = mid, otherwise lo = mid + 1. The production helper adds mid / machine and can return early once the running total meets the goal, which also avoids overflowing a long on very large mid.

Alternatives A min-heap of next-completion times simulates the process directly, but each item would cost a heap operation and that is far heavier than a logarithmic search over days.

Java solution

public class MinimumTimeRequired {
    static long minTime(long[] machines, long goal) {
        long slowest = 0;
        for (long m : machines) slowest = Math.max(slowest, m);
        long lo = 1, hi = slowest * goal; // enough even if only the slowest machine ran
        while (lo < hi) {
            long mid = lo + (hi - lo) / 2;
            if (produced(machines, mid) >= goal) hi = mid;
            else lo = mid + 1;
        }
        return lo;
    }

    static long produced(long[] machines, long days) {
        long items = 0;
        for (long m : machines) {
            items += days / m;
            if (items >= Long.MAX_VALUE / 4) return items; // early exit, already huge
        }
        return items;
    }

    public static void main(String[] args) {
        System.out.println(minTime(new long[] {2, 3}, 5));    // 6
        System.out.println(minTime(new long[] {1, 3, 4}, 10)); // 7
        System.out.println(minTime(new long[] {4, 5, 6}, 12)); // 20
    }
}

Complexity O(n log D) time, where D is the upper bound max(machines) * goal, and O(1) extra space.

Edge cases

  • Official HackerRank samples: machines [2,3] and goal 5 return 6; [1,3,4] and goal 10 return 7; [4,5,6] and goal 12 return 20.
  • A single machine: the answer is machines[0] * goal.

Follow-ups

  • Koko Eating Bananas and Capacity To Ship Packages are the same search on a different feasibility function.
  • If machines could be added or removed over time, the closed-form production no longer holds and you would simulate.

Tests The official HackerRank samples: [2,3] with goal 5 returns 6; [1,3,4] with goal 10 returns 7; [4,5,6] with goal 12 returns 20.

How to say it in the interview "I binary-search the number of days. For each mid I add mid divided by each machine cycle, which is how many items that machine produces in mid days. The first day count that reaches the goal is the answer. I keep the bounds and the totals in longs."

Sources (1)
  1. HackerRank: Minimum Time Requiredhackerrank.com