|
| 1 | +# Schedule Tasks on Minimum Machines |
| 2 | + |
| 3 | +We are given an input array, tasks, where tasks[i]=[starti ,endi] represents the start and end times of n tasks. Our |
| 4 | +goal is to schedule these tasks on machines given the following criteria: |
| 5 | + |
| 6 | +- A machine can execute only one task at a time. |
| 7 | +- A machine can begin executing a new task immediately after completing the previous one. |
| 8 | +- An unlimited number of machines are available. |
| 9 | + |
| 10 | +Find the minimum number of machines required to complete these n tasks. |
| 11 | + |
| 12 | +## Constraints |
| 13 | + |
| 14 | +- n == `tasks.length` |
| 15 | +- 1 <= `tasks.length` <= 10^3 |
| 16 | +- 0 <= `tasksi.start` < `tasksi.end` <= 10^4 |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +The core intuition for solving this problem is to allocate tasks to the minimum number of machines by reusing machines |
| 28 | +whenever possible. The algorithm efficiently manages machine availability by sorting tasks by their start times and using |
| 29 | +a min heap to track end times. If the earliest available machine (top of the heap) finishes before or as a task starts, |
| 30 | +it is reused and removed from the heap. Otherwise, a new machine is allocated, and the current task’s end time is pushed |
| 31 | +into the heap. The heap size at the end represents the minimum number of machines required. |
| 32 | + |
| 33 | +Using the intuition above, we implement the algorithm as follows: |
| 34 | + |
| 35 | +1. Sort the tasks array by the start time of each task to process them in chronological order. |
| 36 | +2. Initialize a min heap (machines) to keep track of the end times of tasks currently using machines. |
| 37 | +3. Iterate over each task in the sorted tasks array. |
| 38 | + - Extract the start and end times of the current task. |
| 39 | + - Check if the machine with the earliest finish time is free, i.e., top of machines is less than or equal to the |
| 40 | + current task’s start time. If it is, remove it from the heap, as the machine can be reused. |
| 41 | + - Push the end time of the current task into the heap, indicating that a machine is now in use until that time. |
| 42 | +4. After processing all tasks, return the size of the heap, which represents the minimum number of machines required. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Time Complexity |
| 65 | + |
| 66 | +The time complexity of the above algorithm is O(nlogn), where n is the number of tasks represented by the length of the |
| 67 | +tasks array. This is because: |
| 68 | + |
| 69 | +- Sorting the array takes O(nlogn). |
| 70 | +- The total cost for heap operations is O(nlogn) because we process n tasks, and each operation on the min-heap has a |
| 71 | + time complexity of O(logn). |
| 72 | + |
| 73 | +Therefore, the overall time complexity is O(nlogn). |
| 74 | + |
| 75 | +### Space Complexity |
| 76 | + |
| 77 | +The algorithm’s space complexity is O(n) because the min heap can grow up to a maximum size of n if every task requires |
| 78 | +a separate machine. |
0 commit comments