1834. Single-Threaded CPU


้้กๅ ถๅฏฆๆ้ป็ฎๅฏฆไฝ้ก, ้ก็ฎๆๆๅฐ
the CPU will choose the one with the shortest processing time.
=> pq (small processingTimei, smallest index)
ๆไปฅ่ฆ็จ pq, ๅ ็บ่ฆๅฐ time, index ๅๆๅบ, ๆไปฅๅไธๅ class ๅบไพๅญๆๆฏๆฏ่ผๆผไบฎ็ไฝๆณ,
Queue<Task> queue = new PriorityQueue<>((a,b) -> {
return a.processingTime == b.processingTime ? a.index - b.index : a.processingTime - b.processingTime;
});
ๅ ็บๆฏ็ งๆ้้ ๅบไพๆพๅ ฅ pq, ๆไปฅ็ถ็ถ้ๅงๆ้่ฆๅ ๆๅบ
Arrays.sort(jobs, (a,b) -> a.enqueueTime - b.enqueueTime);
ๆฏ่ผไธๅฅฝ่จญ่จ็้จๅๆฏ, ๅฆไฝๆ็ ง ๅๅๆ้ ๅ ฅ pq ๅข๏ผ
from the name enqueueTime => hint us can add to queue
้้ป่ฆๆๅ curTime ไพ่จ้็ฎๅๆ้ๅฐๅช่ฃกไบ, pq ๆฏ็ฉบ็ๆๅ, ๆ็ตฆไบๅๅผ๏ผ็ฌฌไธๅjob ็่ตทๅงๆ้๏ผ
ๅ ถไป pq ๆๅ ็ด ๆ, ็ถ็ถๅฐฑๆฏๆดๆฐ็ตๆๅๆดๆฐ curTime (ๅ ไธ processingTime, ๅ ็บ้ๆฏไธๅ single thread ็ CPU, ไธๆฌกๅไธไปถไบ)
from array to queue
ไน็ถญๆไบไธๅ runIdx ไพ่ฎไธๅ job, ๆขไปถๆฏ ่ตทๅงๆ้ <= curTime
while()...้่ฃ
ๅฆๆญคๅฐฑๅฏไปฅๆ็ฌฆๅ่ตทๅงๆ้ ็job ๅ ฅ pq
int[] order = new int[n];
int orderIdx = 0;
int runIdx = 0;
int curTime = 0;
while (orderIdx < n) {
while (runIdx < n && jobs[runIdx].enqueueTime <= curTime) {
queue.offer(jobs[runIdx]);
runIdx++;
}
// when no data in queue, curTime needs to refresh
if (queue.isEmpty()) {
curTime = jobs[runIdx].enqueueTime;
} else {
Task cur = queue.poll();
order[orderIdx++] = cur.index;
curTime += cur.processingTime;
}
}
return order;
T: O(nlogn)
S: O(n)
class Solution {
class Task {
int index;
int enqueueTime;
int processingTime;
Task(int index,
int enqueueTime,
int processingTime) {
this.index = index;
this.enqueueTime = enqueueTime;
this.processingTime = processingTime;
}
}
public int[] getOrder(int[][] tasks) {
int n = tasks.length;
Task[] jobs = new Task[n];
for (int i = 0; i < n; i++) {
jobs[i] = new Task(i, tasks[i][0], tasks[i][1]);
}
Arrays.sort(jobs, (a,b) -> a.enqueueTime - b.enqueueTime);
Queue<Task> queue = new PriorityQueue<>((a,b) -> {
return a.processingTime == b.processingTime ? a.index - b.index : a.processingTime - b.processingTime;
});
int[] order = new int[n];
int orderIdx = 0;
int runIdx = 0;
int curTime = 0;
while (orderIdx < n) {
while (runIdx < n && jobs[runIdx].enqueueTime <= curTime) {
queue.offer(jobs[runIdx]);
runIdx++;
}
// when no data in queue, curTime needs to refresh
if (queue.isEmpty()) {
curTime = jobs[runIdx].enqueueTime;
} else {
Task cur = queue.poll();
order[orderIdx++] = cur.index;
curTime += cur.processingTime;
}
}
return order;
}
}
/*
tasks[i] = [enqueueTimei, processingTimei]
ith task
process at enqueueTimei, needs processingTimei
the CPU will choose the one with the shortest processing time.
=> pq (small processingTimei, smallest index)
finished entire task
Return the order in which the CPU will process the tasks. (index)
[[1,2],[2,4],[3,2],[4,1]]
1. build task class
2. define pq
curTime = 0;
3. while (orderId < n) {
while (runId < n && eTime <= curTime) {
add to heap
runId++
}
if (heap is empty) {
init curTime = task[runId].eTime
}
heap pop, get cur task
add to result[orderId++]
curTime += cur task.proTime
}
T: O(nlogn)
S: O(n)
*/
Last updated
Was this helpful?