File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Priority_Queue/1882.Process-Tasks-Using-Servers Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ typedef array<int ,2 > AI2;
2+ typedef array<int ,3 > AI3;
3+
4+ class Solution {
5+ public:
6+ vector<int > assignTasks (vector<int >& servers, vector<int >& tasks)
7+ {
8+ priority_queue<AI2, vector<AI2>, greater<>>pq1;
9+ priority_queue<AI3, vector<AI3>, greater<>>pq2;
10+ for (int i=0 ; i<servers.size (); i++)
11+ pq1.push ({servers[i], i});
12+
13+
14+ queue<int >jobs;
15+ vector<int >rets (tasks.size (),-1 );
16+
17+ for (int j=0 ; j<tasks.size (); j++)
18+ {
19+ jobs.push (j);
20+ while (!pq2.empty () && pq2.top ()[0 ] <= j)
21+ {
22+ auto [t,w,idx] = pq2.top ();
23+ pq2.pop ();
24+ pq1.push ({w,idx});
25+ }
26+
27+ while (!jobs.empty () && !pq1.empty ())
28+ {
29+ int job = jobs.front ();
30+ jobs.pop ();
31+ auto [w, idx] = pq1.top ();
32+ pq1.pop ();
33+ pq2.push ({j+tasks[job], w, idx});
34+ rets[job] = idx;
35+ }
36+ }
37+
38+ while (!jobs.empty ())
39+ {
40+ int job = jobs.front ();
41+ jobs.pop ();
42+ auto [t, w, idx] = pq2.top ();
43+ pq2.pop ();
44+ pq2.push ({t+tasks[job], w, idx});
45+ rets[job] = idx;
46+ }
47+
48+ return rets;
49+ }
50+ };
You can’t perform that action at this time.
0 commit comments