# Process Tasks Using Servers solution leetcode

## Process Tasks Using Servers solution leetcode

You are given two 0-indexed integer arrays `servers` and `tasks` of lengths `n`​​​​​​ and `m`​​​​​​ respectively. `servers[i]` is the weight of the `i​​​​​​th`​​​​ server, and `tasks[j]` is the time needed to process the `j​​​​​​th`​​​​ task in seconds.

You are running a simulation system that will shut down after all tasks are processed. Each server can only process one task at a time. You will be able to process the `jth` task starting from the `jth` second beginning with the `0th` task at second `0`. To process task `j`, you assign it to the server with the smallest weight that is free, and in case of a tie, choose the server with the smallest index. If a free server gets assigned task `j` at second `t`,​​​​​​ it will be free again at the second `t + tasks[j]`.

If there are no free servers, you must wait until one is free and execute the free tasks as soon as possible. If multiple tasks need to be assigned, assign them in order of increasing index.

You may assign multiple tasks at the same second if there are multiple free servers.

Build an array `ans`​​​​ of length `m`, where `ans[j]` is the index of the server the `j​​​​​​th` task will be assigned to.

Return the array `ans`​​​​.

Process Tasks Using Servers solution leetcode

Example 1: Process Tasks Using Servers solution leetcode

```Input: servers = [3,3,2], tasks = [1,2,3,2,1,2]
Output: [2,2,0,2,1,2]
Explanation: Events in chronological order go as follows:
- At second 0, task 0 is added and processed using server 2 until second 1.
- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.
- At second 2, task 2 is added and processed using server 0 until second 5.
- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.
- At second 4, task 4 is added and processed using server 1 until second 5.
- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.```

Example 2: Process Tasks Using Servers solution leetcode

```Input: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
Output: [1,4,1,4,1,3,2]
Explanation: Events in chronological order go as follows:
- At second 0, task 0 is added and processed using server 1 until second 2.
- At second 1, task 1 is added and processed using server 4 until second 2.
- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4.
- At second 3, task 3 is added and processed using server 4 until second 7.
- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9.
- At second 5, task 5 is added and processed using server 3 until second 7.
- At second 6, task 6 is added and processed using server 2 until second 7.
```

Process Tasks Using Servers solution leetcode

Constraints: Process Tasks Using Servers solution leetcode

• `servers.length == n`
• `tasks.length == m`
• `1 <= n, m <= 2 * 105`
• `1 <= servers[i], tasks[j] <= 2 * 105`

# SOLUTION

### Welcome to the 243th LeetCode Weekly Contest

#### Important Note

1. To provide a better contest and ensure fairness, we listened to LeetCoders’ feedback and put in lots of thoughts behind the updated contest rule. Please check out our new contest rule which covers more scenarios with details explained.
2. The penalty time of 5 minutes will be applied for each wrong submission.
3. To ensure the fairness of the contest, LeetCode will hide some test cases during the contest. When users submit incorrect submissions, LeetCode will not show the hidden test cases to the users.
4. The final rating of this contest will be updated within 5 working days after the contest.

#### Below actions are deemed contest violations

1. :

• One user submitting with multiple accounts during a contest. LCUS (leetcode.com) account and LCCN (leetcode-cn.com) account are considered to be separate accounts, even if both accounts belong to the same user.
• Multiple accounts submitting similar code for the same problem.
• Creating unwanted disturbances which interrupt other users’ participation in a contest.
• Disclosing contest solutions in public discuss posts before the end of a contest.
1. LeetCode heavily emphasizes on the justice and fairness of our contests. We have absolutely

#### ZERO TOLERANCE

1.  for violation behaviors (such as plagiarism, cheating, etc). When a user is deemed violating contest rules, we will apply the following penalties on this user:

• First violation: LeetCoin amount resets to zero and a contest and discuss ban for 1 month.
• Second violation: Permanent account deactivation without appeal.
Furthermore, we encourage all participants to contribute to maintaining the justice and fairness of our contest. Users who submit valid violation report(s) will earn additional LeetCoins:

• For each violating participant, the first 10 users who submit the violation report towards this participant will each earn 20 LeetCoins.
• Each user can earn up to 100 LeetCoins for reporting violations in a contest.
• Users will not be rewarded LeetCoins for reports on LCCN users.