Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int allocateResources(int n, List<Integer> list) {
- int[] burstTime= new int[list.size()];
- int i=0;
- for(int x:list)burstTime[i++]=x;
- int m = burstTime.length;
- int low = Arrays.stream(burstTime).max().getAsInt();
- int high = Arrays.stream(burstTime).sum();
- int result = 0;
- while (low <= high) {
- int mid = low + (high - low) / 2;
- if (isPossibleToAllocate(n, m, burstTime, mid)) {
- result = mid;
- high = mid - 1;
- } else {
- low = mid + 1;
- }
- }
- return result;
- }
- public static boolean isPossibleToAllocate(int n, int m, int[] burstTime, int maxLoad) {
- int currentLoad = 0;
- int serversUsed = 1;
- for (int i = 0; i < m; i++) {
- if (burstTime[i] > maxLoad) {
- return false; // A single task cannot be allocated to a server with load capacity less than its processing time.
- }
- if (currentLoad + burstTime[i] > maxLoad) {
- serversUsed++;
- currentLoad = 0;
- }
- currentLoad += burstTime[i];
- }
- return serversUsed <= n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement