Skip to content

Commit 218ba28

Browse files
authored
Update 1834-single-threaded-cpu.js
1 parent d3e5f08 commit 218ba28

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

1834-single-threaded-cpu.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,46 @@ const getOrder = function(tasks) {
239239
return res
240240

241241
};
242+
243+
// another
244+
245+
/**
246+
* @param {number[][]} tasks
247+
* @return {number[]}
248+
*/
249+
const getOrder = function (tasks) {
250+
tasks = tasks.map((e, idx) => [e[0], e[1], idx])
251+
tasks.sort((a, b) => a[0] - b[0])
252+
const pq = new PriorityQueue(compare)
253+
const res = []
254+
let i = 0,
255+
t = 0
256+
while (i < tasks.length) {
257+
while (i < tasks.length && tasks[i][0] <= t) {
258+
let [ent, pt, ind] = tasks[i]
259+
i += 1
260+
pq.push([pt, ind])
261+
}
262+
if (pq.size() == 0) {
263+
if (i < tasks.length) t = tasks[i][0]
264+
continue
265+
}
266+
let [pt, ind] = pq.pop()
267+
res.push(ind)
268+
t += pt
269+
}
270+
while (pq.size()) {
271+
let [pt, index] = pq.pop()
272+
res.push(index)
273+
}
274+
return res
275+
}
276+
277+
function compare(a, b) {
278+
if (a[0] < b[0]) return true
279+
else if (a[0] > b[0]) return false
280+
else {
281+
return a[1] < b[1]
282+
}
283+
}
284+

0 commit comments

Comments
 (0)