Skip to content

Commit c3e4c76

Browse files
authored
Update 1834-single-threaded-cpu.js
1 parent faec736 commit c3e4c76

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1834-single-threaded-cpu.js

+30
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,33 @@ class PriorityQueue {
209209
}
210210
}
211211
}
212+
213+
// another
214+
/**
215+
* @param {number[][]} tasks
216+
* @return {number[]}
217+
*/
218+
const getOrder = function(tasks) {
219+
const n = tasks.length
220+
const pq = new PriorityQueue((a, b) => a[0] === b[0] ? a[1] < b[1] : a[0] < b[0])
221+
tasks.forEach((e, i) => e.push(i))
222+
tasks.sort((a, b) => a[0] - b[0])
223+
let idx = 0, time = 0
224+
const res = []
225+
226+
while(idx < n || !pq.isEmpty()) {
227+
while(idx < n && tasks[idx][0] <= time) {
228+
pq.push([tasks[idx][1], task[idx][2]])
229+
idx++
230+
}
231+
if(!pq.isEmpty()) {
232+
const tmp = pq.pop()
233+
time += tmp[0]
234+
res.push(tmp[1])
235+
} else if(idx < n) {
236+
time = tasks[idx][0]
237+
}
238+
}
239+
return res
240+
241+
};

0 commit comments

Comments
 (0)