Advertisement
rajeshinternshala

Untitled

Jul 31st, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1.     public static int allocateResources(int n, List<Integer> list) {
  2.         int[] burstTime= new int[list.size()];
  3.         int i=0;
  4.         for(int x:list)burstTime[i++]=x;
  5.         int m = burstTime.length;
  6.         int low = Arrays.stream(burstTime).max().getAsInt();
  7.         int high = Arrays.stream(burstTime).sum();
  8.         int result = 0;
  9.  
  10.         while (low <= high) {
  11.             int mid = low + (high - low) / 2;
  12.             if (isPossibleToAllocate(n, m, burstTime, mid)) {
  13.                 result = mid;
  14.                 high = mid - 1;
  15.             } else {
  16.                 low = mid + 1;
  17.             }
  18.         }
  19.  
  20.         return result;
  21.     }
  22.  
  23.     public static boolean isPossibleToAllocate(int n, int m, int[] burstTime, int maxLoad) {
  24.         int currentLoad = 0;
  25.         int serversUsed = 1;
  26.  
  27.         for (int i = 0; i < m; i++) {
  28.             if (burstTime[i] > maxLoad) {
  29.                 return false; // A single task cannot be allocated to a server with load capacity less than its processing time.
  30.             }
  31.  
  32.             if (currentLoad + burstTime[i] > maxLoad) {
  33.                 serversUsed++;
  34.                 currentLoad = 0;
  35.             }
  36.  
  37.             currentLoad += burstTime[i];
  38.         }
  39.  
  40.         return serversUsed <= n;
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement