Untitled
unknown
java
a year ago
1.5 kB
5
Indexable
import java.util.Collections;
import java.util.PriorityQueue;
public class Solution {
public static int minimumTime(int[] ability, long processes) {
// Create a max-heap using a priority queue
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (int a : ability) {
maxHeap.offer(a);
}
int time = 0;
while (processes > 0) {
if (maxHeap.isEmpty()) {
// All processors have zero ability left
break;
}
// Get the processor with the maximum ability
int maxAbility = maxHeap.poll();
// Schedule processes equal to the minimum of remaining processes and maxAbility
long scheduled = Math.min(processes, maxAbility);
processes -= scheduled;
// Reduce the processor's ability to floor(ability / 2)
int newAbility = maxAbility / 2;
if (newAbility > 0) {
maxHeap.offer(newAbility);
}
// Increment time after each scheduling
time++;
}
return time;
}
public static void main(String[] args) {
// Use sample input
int n = 5;
int[] ability = {2, 1, 5, 3, 1};
long processes = 17;
int result = minimumTime(ability, processes);
System.out.println(result); // Expected output: 9
}
}
Editor is loading...
Leave a Comment