Process Scheduling
A multiprocessor system contains several processors, where ability[i] represents the number of processes the ith processor can schedule in one second.
Problem Description
In each second, you can choose any one processor and use it to schedule up to its current ability.
Once a processor is used:
It schedules the processes (up to its maximum current capacity).
Its ability is reduced to ⌊ability/2⌋.
The goal is to determine the minimum time (in seconds) required to schedule all given processes.
Example
Input:
ability = [3, 1, 7, 2, 4]
processes = 15
Output: 4
Explanation:
Second 1: Use processor with ability 7. Processes remaining: 15−7=8. New ability: ⌊7/2⌋=3.
Second 2: Use processor with ability 4. Processes remaining: 8−4=4. New ability: ⌊4/2⌋=2.
Second 3: Use processor with ability 3. Processes remaining: 4−3=1. New ability: ⌊3/2⌋=1.
Second 4: Use processor with ability 1. Processes remaining: 1−1=0.
Total time taken: 4 seconds.
Constraints
1≤ size of ability ≤10^5
1≤ ability[i] ≤10^6
1≤ processes ≤10^12
It is guaranteed that the processes can be scheduled using the given system.
Solution:
public static int minimumTime(List<Integer> ability, long processes) {
int ans = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int a: ability) {
pq.add(a);
}
long rem = processes;
while(!pq.isEmpty()) {
Integer pr = pq.poll();
rem = rem - pr;
pr = pr/2;
if(pr != 0)
pq.offer(pr);
ans++;
if(rem<= 0) {
break;
}
}
return ans;
}
}