710142. Minimum time to fulfil all orders

Hard

Minimum time to fulfil all orders

Slug: minimum-time-to-fulfil-all-orders

Difficulty: Hard

Topic Tags: Binary Search, Algorithms

Company Tags: None

Summary

Given a list of tasks with their corresponding deadlines and a single machine with an available capacity, the problem is to find the minimum time required to complete all tasks. The tasks are represented as intervals on a timeline, and the goal is to schedule them in a way that minimizes the total time taken.

Detailed Explanation

The given task intervals can be thought of as a set of events that need to be scheduled. We can use binary search to find the minimum time required to complete all tasks. The idea is to start with a guess for the minimum time and then adjust it based on whether we can schedule all tasks within that time or not.

Here's a step-by-step breakdown of the solution:

  1. Initialize a variable low as 0, which represents the earliest possible deadline.
  2. Initialize a variable high as the maximum deadline, which represents the latest possible deadline.
  3. Perform binary search to find the minimum time required to complete all tasks.
    • Calculate the mid-time by averaging low and high.
    • Check if it's possible to schedule all tasks within the given time frame (i.e., if the sum of task durations is less than or equal to the mid-time).
      • If yes, update high as the mid-time.
      • If no, update low as the mid-time.
  4. Repeat step 3 until high and low converge.

The time complexity of this algorithm is O(log N), where N is the number of tasks. The space complexity is O(1).

Optimized Solutions

Java

View on GeeksforGeeks
public int minTime(int[][] orders) {
    Arrays.sort(orders, (a, b) -> a[0] - b[0]);
    
    int low = 0;
    int high = orders[orders.length - 1][1];
    
    while (low < high) {
        int mid = (low + high) / 2;
        
        boolean canSchedule = true;
        int currentTime = 0;
        for (int[] order : orders) {
            if (order[0] > mid) break;
            if (currentTime >= order[1]) continue;
            currentTime += order[0];
        }
        
        if (!canSchedule) high = mid;
        else low = mid + 1;
    }
    
    return low;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.