Skip to content

Commit e7e446d

Browse files
authored
Update 1882-process-tasks-using-servers.js
1 parent 92cf1f6 commit e7e446d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

1882-process-tasks-using-servers.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,56 @@ class PriorityQueue {
116116
}
117117
}
118118
}
119+
120+
// another
121+
122+
/**
123+
* @param {number[]} servers
124+
* @param {number[]} tasks
125+
* @return {number[]}
126+
*/
127+
const assignTasks = function(servers, tasks) {
128+
const freePQ = new PriorityQueue((a, b) => {
129+
if(a.w < b.w) return true
130+
else if(a.w > b.w) return false
131+
else {
132+
if(a.idx < b.idx) return true
133+
return false
134+
}
135+
})
136+
const runningPQ = new PriorityQueue((a, b) => {
137+
return a.end === b.end ? (a.w === b.w ? a.idx < b.idx : a.w < b.w) : a.end < b.end
138+
})
139+
const res = []
140+
for(let i = 0; i < servers.length; i++) {
141+
freePQ.push({
142+
w: servers[i],
143+
idx: i
144+
})
145+
}
146+
for(let i = 0, n = tasks.length; i < n; i++) {
147+
const cur = tasks[i]
148+
while(runningPQ.size() && runningPQ.peek().end <= i) {
149+
const el = runningPQ.pop()
150+
freePQ.push({
151+
w: el.w,
152+
idx: el.idx,
153+
})
154+
}
155+
156+
if(freePQ.isEmpty()) {
157+
const el = runningPQ.pop()
158+
res[i] = el.idx
159+
el.end += cur
160+
runningPQ.push(el)
161+
} else {
162+
const el = freePQ.pop()
163+
res[i] = el.idx
164+
el.end = i + cur
165+
runningPQ.push(el)
166+
}
167+
}
168+
169+
return res
170+
};
171+

0 commit comments

Comments
 (0)