题目
https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/
Javascript 暴力破解
var busiestServers = function (k, arrival, load) {
let arr = []
for (let i = 0; i < k; i++) {
arr.push({
excuteTime: 0,
excuteTask: 0,
})
}
for (let j = 0; j < arrival.length; j++) {
let current = j % k
let isJoin = false
while (current < k) {
if (arr[current].excuteTime - arrival[j] <= 0) {
arr[current].excuteTime = arrival[j] + load[j]
arr[current].excuteTask += 1
isJoin = true
break
}
current++
}
if (!isJoin) {
let start = 0
while (start < j % k) {
if (arr[start].excuteTime - arrival[j] <= 0) {
arr[start].excuteTime = arrival[j] + load[j]
arr[start].excuteTask += 1
isJoin = true
break
}
start++
}
}
}
let result = []
let num = 0
for (let i = 0; i < k; i++) {
if (arr[i].excuteTask > num) {
num = arr[i].excuteTask
result = [i]
} else if (arr[i].excuteTask == num) {
result.push(i)
}
}
return result
}
测试
k | arraive | load | 输出 |
---|
3 | [1, 2, 3, 4, 5] | [5, 2, 3, 3, 3] | [ 1 ] | 3 | [1, 2, 3, 4, 8, 9, 10] | [5, 2, 10, 3, 1, 2, 2] | [ 1 ] | 1 | [1] | [1] | [ 0 ] | 3 | [1, 2, 3] | [10, 12, 11] | [ 0, 1, 2 ] | 3 | [1, 2, 3, 4] | [1, 2, 1, 2] | [ 0 ] | 2 | [2, 3, 4, 8] | [3, 2, 4, 3] | [ 1 ] | 7 | [1, 3, 4, 5, 6, 11, 12, 13, 15, 19, 20, 21, 23, 25, 31, 32] | [9, 16, 14, 1, 5, 15, 6, 10, 1, 1, 7, 5, 11, 4, 4, 6] | [ 0 ] |
|