---
tags: leetcode
---
# [1882. Process Tasks Using Servers](https://leetcode.com/problems/process-tasks-using-servers/)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
struct serverData {
int weight;
int index;
int availableTime;
serverData(int weight, int index, int availableTime):
weight(weight), index(index), availableTime(availableTime) {}
};
class availableCmp {
public:
bool operator() (serverData &s1, serverData &s2) {
if (s1.weight > s2.weight) {
return true;
} else if (s1.weight < s2.weight) {
return false;
}
// s1.weight == s2.weight is true
return s1.index > s2.index;
}
};
class busyCmp {
public:
bool operator() (serverData &s1, serverData &s2) {
if (s1.availableTime > s2.availableTime) {
return true;
} else if (s1.availableTime < s2.availableTime) {
return false;
}
// s1.availableTime == s2.availableTime is true
if (s1.weight > s2.weight) {
return true;
} else if (s1.weight < s2.weight) {
return false;
}
// s1.weight == s2.weight is true
return s1.index > s2.index;
}
};
class Solution {
public:
vector<int> assignTasks(vector<int> &servers, vector<int> &tasks) {
priority_queue<serverData, vector<serverData>, availableCmp> availableQ;
for (int i = 0; i < servers.size(); ++i) {
availableQ.push(serverData(servers[i], i, 0));
}
vector<int> answer;
priority_queue<serverData, vector<serverData>, busyCmp> busyQ;
int taskIdx = 0;
while (taskIdx < tasks.size()) {
while (!busyQ.empty() && busyQ.top().availableTime <= taskIdx) {
availableQ.push(busyQ.top());
busyQ.pop();
}
if (!availableQ.empty()) {
serverData availableServer = availableQ.top();
availableQ.pop();
answer.push_back(availableServer.index);
availableServer.availableTime = taskIdx + tasks[taskIdx];
busyQ.push(availableServer);
} else {
// no avaliable server
serverData busyServer = busyQ.top();
busyQ.pop();
answer.push_back(busyServer.index);
busyServer.availableTime += tasks[taskIdx];
busyQ.push(busyServer);
}
++taskIdx;
}
return answer;
}
};
```
## Time Complexity
$O(nlogn + mlogn)$
$n$ is the length of `servers`.
$m$ is the length of `tasks`.
## Space Complexity
$O(n)$
# Miscellane
<!--
# Test Cases
```
3,3,2]
[1,2,3,2,1,2]
```
```
[5,1,4,3,2]
[2,1,2,4,5,2,1]
```
* Wrong Answer
```
[10,63,95,16,85,57,83,95,6,29,71]
[70,31,83,15,32,67,98,65,56,48,38,90,5]
```
* Wrong Answer
```
[31,96,73,90,15,11,1,90,72,9,30,88]
[87,10,3,5,76,74,38,64,16,64,93,95,60,79,54,26,30,44,64,71]
```
* Wrong Answer
```
[338,890,301,532,284,930,426,616,919,267,571,140,716,859,980,469,628,490,195,664,925,652,503,301,917,563,82,947,910,451,366,190,253,516,503,721,889,964,506,914,986,718,520,328,341,765,922,139,911,578,86,435,824,321,942,215,147,985,619,865]
[773,537,46,317,233,34,712,625,336,221,145,227,194,693,981,861,317,308,400,2,391,12,626,265,710,792,620,416,267,611,875,361,494,128,133,157,638,632,2,158,428,284,847,431,94,782,888,44,117,489,222,932,494,948,405,44,185,587,738,164,356,783,276,547,605,609,930,847,39,579,768,59,976,790,612,196,865,149,975,28,653,417,539,131,220,325,252,160,761,226,629,317,185,42,713,142,130,695,944,40,700,122,992,33,30,136,773,124,203,384,910,214,536,767,859,478,96,172,398,146,713,80,235,176,876,983,363,646,166,928,232,699,504,612,918,406,42,931,647,795,139,933,746,51,63,359,303,752,799,836,50,854,161,87,346,507,468,651,32,717,279,139,851,178,934,233,876,797,701,505,878,731,468,884,87,921,782,788,803,994,67,905,309,2,85,200,368,672,995,128,734,157,157,814,327,31,556,394,47,53,755,721,159,843]
```
-->