Skip to content

Commit 8fa8883

Browse files
committed
Add solution #1606
1 parent 7e51cfe commit 8fa8883

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,480 LeetCode solutions in JavaScript
1+
# 1,481 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1240,6 +1240,7 @@
12401240
1603|[Design Parking System](./solutions/1603-design-parking-system.js)|Easy|
12411241
1604|[Alert Using Same Key-Card Three or More Times in a One Hour Period](./solutions/1604-alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.js)|Medium|
12421242
1605|[Find Valid Matrix Given Row and Column Sums](./solutions/1605-find-valid-matrix-given-row-and-column-sums.js)|Medium|
1243+
1606|[Find Servers That Handled Most Number of Requests](./solutions/1606-find-servers-that-handled-most-number-of-requests.js)|Hard|
12431244
1608|[Special Array With X Elements Greater Than or Equal X](./solutions/1608-special-array-with-x-elements-greater-than-or-equal-x.js)|Easy|
12441245
1609|[Even Odd Tree](./solutions/1609-even-odd-tree.js)|Medium|
12451246
1610|[Maximum Number of Visible Points](./solutions/1610-maximum-number-of-visible-points.js)|Hard|
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)