Minimum Time Required

Sort by

recency

|

155 Discussions

|

  • + 1 comment
    def minTime(machines, goal):
        machines.sort()
        worst = machines[-1] * goal//len(machines)
        best = machines[0] * goal//len(machines)
        result = -1
        while worst>best+1:
            mid = best+(worst - best) // 2
            count = 0
            for machine in machines:
                count += mid//machine
            if count >=goal:
                worst = mid
                result = mid
            else:
                best = mid
        return result
    
  • + 0 comments

    ez

    let M be the min element of machines, then M*goal is 10^18 at most = 2^64 at most

    O(n * log(M * goal)), the log cannot exceed 64

    long production(long days, const vector<long>& machines) {
        long sum = 0;
        for (long x : machines) sum = sum + floor(days / x);
        return sum;
    }
    
    long minTime(vector<long> machines, long goal) {
        long low = 0, high = *min_element(machines.begin(), machines.end()) * goal;
        while (low < high) {
            long mid = (low + high) / 2;
            if (production(mid, machines) < goal) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    
  • + 0 comments

    why is this TLE? I am using binary search and the last for loop -- couldn't possibly go too much back.

    long minTime(vector<long> machines, long goal) {
        //binary search
        // check if sum of machines = goal or < or >
        
        int n = machines.size();
        long fastest = *min_element(machines.begin(), machines.end());
        
        long low = 0, high = goal*fastest;
        long mid = (low+high)/2;
        long sum = 0;
        while(high >= low)
        {
            sum = 0;
            for(int i=0; i<n; i++)
            {
                sum += (mid/machines[i]);
            }
            
            if(sum == goal) break;
            else if(sum > goal)
            {
                high = mid;
            }
            else {
                low = mid;
            }
            mid = (low+high)/2;
        }
        
        int num = mid;
        while(sum == goal)
        {
            num--;
            sum = 0;
            for(int i=0; i<n; i++)
            {
                sum += (num/machines[i]);
            }
            if(sum != goal)
            {
                num++;
                break;
            }
        }
        return num;
    }
    
  • + 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
    
  • + 0 comments

    JS binary search

    function minTime(machines, goal) {
      let min = 1, max = goal * Math.max(...machines)
    
      while (min < max) {
        let mid = parseInt((min + max) / 2)
        let prod = machines.reduce((prev, curr) => prev + parseInt(mid / curr), 0)
        if (prod < goal) min = mid + 1
        else max = mid
      }
      return min
    }