Skip to content

Commit 6d3e84b

Browse files
committed
Runtime: 1039 ms (Top 46.64%) | Memory: 141.9 MB (Top 37.92%)
1 parent 6d16d29 commit 6d3e84b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Runtime: 1039 ms (Top 46.64%) | Memory: 141.9 MB (Top 37.92%)
2+
class Solution {
3+
public:
4+
vector<int> getOrder(vector<vector<int>>& tasks) {
5+
6+
//we use priority queue to get the least processing time from the available server
7+
//implement a min heap
8+
9+
//dp is double pair
10+
//sp is single pair
11+
using dp=pair<long int,pair<long int,long int >> ;
12+
using sp=pair<long int,long int>;
13+
priority_queue<sp,vector<sp>,greater<sp>> pq;
14+
int len=tasks.size();
15+
//we can rearrage the tasks but we can't get the index in the original array
16+
vector<dp> rearrange;
17+
for(long int i=0;i<len;i++)
18+
{
19+
rearrange.push_back({tasks[i][0],{tasks[i][1],i}});
20+
}
21+
22+
//rearrange contains the same as tasks but with extra value "index" in its original array
23+
24+
//sort in the ascending order of their enqueue time
25+
//if two tasks have same enqueue time it will sort the one which has the less processing time
26+
sort(rearrange.begin(),rearrange.end());
27+
long int i=0;
28+
long int finishTime=rearrange[0].first;
29+
long int k=tasks.size();
30+
31+
vector<int> res;
32+
while(k)
33+
{
34+
while(i<len && finishTime>=rearrange[i].first)
35+
{
36+
//push the processing time and the index
37+
pq.push({rearrange[i].second.first,rearrange[i].second.second});
38+
i++;
39+
}
40+
41+
//pick the task which is available upto the current finishTime and with the less processing time
42+
auto [time,ind]=pq.top();
43+
pq.pop();
44+
45+
//processing the tasks take "time"
46+
finishTime+=time; //the cpu is now idle at the time finishTime
47+
res.push_back(ind);
48+
49+
//now i points to the next task
50+
//if there are no tasks left in pq
51+
// and the next tasks enqueue time is larger than the current finishing time
52+
//we start with the task enqueue time
53+
if(pq.empty() && (i<len && finishTime < rearrange[i].first ))
54+
finishTime=rearrange[i].first;
55+
56+
k--;
57+
}
58+
return res;
59+
60+
}
61+
};

0 commit comments

Comments
 (0)