Minimum Time Required

  • + 0 comments

    The binary search is certainly a great idea, but solutions I see all start from 0 or 1. The minum days is fastest-machine/number-of-machines * item_targets and the maxium days is slowest-machine/number-of-machines * item_targets:

    def min_time(machines, num_items_goal):
        fastest_machine = min(machines)
        slowest_machine = max(machines)
        fastest_production_num_days_per_item = fastest_machine // len(machines)
        slowest_production_num_days_per_item = slowest_machine // len(machines) + 1
        min_days, max_days = \
            fastest_production_num_days_per_item * num_items_goal, \
            slowest_production_num_days_per_item * num_items_goal
        while max_days - min_days > 1:
            mid = (max_days + min_days) // 2
            item_cnt = 0
            for m in machines:
                item_cnt += mid // m
            if item_cnt < num_items_goal:
                min_days = mid
            else:
                max_days = mid
        return max_days  # we return the bigger number