|
| 1 | +/** |
| 2 | + * 1606. Find Servers That Handled Most Number of Requests |
| 3 | + * https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You have k servers numbered from 0 to k-1 that are being used to handle multiple requests |
| 7 | + * simultaneously. Each server has infinite computational capacity but cannot handle more than |
| 8 | + * one request at a time. The requests are assigned to servers according to a specific algorithm: |
| 9 | + * - The ith (0-indexed) request arrives. |
| 10 | + * - If all servers are busy, the request is dropped (not handled at all). |
| 11 | + * - If the (i % k)th server is available, assign the request to that server. |
| 12 | + * - Otherwise, assign the request to the next available server (wrapping around the list of servers |
| 13 | + * and starting from 0 if necessary). For example, if the ith server is busy, try to assign the |
| 14 | + * request to the (i+1)th server, then the (i+2)th server, and so on. |
| 15 | + * |
| 16 | + * You are given a strictly increasing array arrival of positive integers, where arrival[i] |
| 17 | + * represents the arrival time of the ith request, and another array load, where load[i] represents |
| 18 | + * the load of the ith request (the time it takes to complete). Your goal is to find the busiest |
| 19 | + * server(s). A server is considered busiest if it handled the most number of requests successfully |
| 20 | + * among all the servers. |
| 21 | + * |
| 22 | + * Return a list containing the IDs (0-indexed) of the busiest server(s). You may return the IDs |
| 23 | + * in any order. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number} k |
| 28 | + * @param {number[]} arrival |
| 29 | + * @param {number[]} load |
| 30 | + * @return {number[]} |
| 31 | + */ |
| 32 | +var busiestServers = function(k, arrival, load) { |
| 33 | + const requests = new Array(k).fill(0); |
| 34 | + const busy = new PriorityQueue((a, b) => a[0] - b[0]); |
| 35 | + const right = Array.from({length: k}, (_, i) => i); |
| 36 | + let left = []; |
| 37 | + |
| 38 | + for (let i = 0; i < arrival.length; i++) { |
| 39 | + const time = arrival[i]; |
| 40 | + const target = i % k; |
| 41 | + |
| 42 | + while (!busy.isEmpty() && busy.front()[0] <= time) { |
| 43 | + const serverId = busy.dequeue()[1]; |
| 44 | + (serverId >= target) ? binaryInsert(right, serverId) : binaryInsert(left, serverId); |
| 45 | + } |
| 46 | + |
| 47 | + let assigned = -1; |
| 48 | + |
| 49 | + if (right.length > 0) { |
| 50 | + const index = binarySearch(right, target); |
| 51 | + if (index < right.length) { |
| 52 | + assigned = right[index]; |
| 53 | + right.splice(index, 1); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (assigned === -1 && left.length > 0) { |
| 58 | + assigned = left[0]; |
| 59 | + left.shift(); |
| 60 | + } |
| 61 | + |
| 62 | + if (assigned !== -1) { |
| 63 | + requests[assigned]++; |
| 64 | + busy.enqueue([time + load[i], assigned]); |
| 65 | + } |
| 66 | + |
| 67 | + if ((i + 1) % k === 0) { |
| 68 | + right.push(...left); |
| 69 | + right.sort((a, b) => a - b); |
| 70 | + left = []; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const maxRequests = Math.max(...requests); |
| 75 | + return requests.reduce((result, count, index) => { |
| 76 | + if (count === maxRequests) result.push(index); |
| 77 | + return result; |
| 78 | + }, []); |
| 79 | +}; |
| 80 | + |
| 81 | +function binarySearch(arr, target) { |
| 82 | + let left = 0; |
| 83 | + let right = arr.length - 1; |
| 84 | + let result = arr.length; |
| 85 | + |
| 86 | + while (left <= right) { |
| 87 | + const mid = Math.floor((left + right) / 2); |
| 88 | + if (arr[mid] >= target) { |
| 89 | + result = mid; |
| 90 | + right = mid - 1; |
| 91 | + } else { |
| 92 | + left = mid + 1; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return result; |
| 97 | +} |
| 98 | + |
| 99 | +function binaryInsert(arr, val) { |
| 100 | + arr.splice(binarySearch(arr, val), 0, val); |
| 101 | +} |
0 commit comments