返回介绍

solution / 1800-1899 / 1882.Process Tasks Using Servers / README_EN

发布于 2024-06-17 01:03:13 字数 5988 浏览 0 评论 0 收藏 0

1882. Process Tasks Using Servers

中文文档

Description

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.

Tasks are assigned to the servers using a task queue. Initially, all servers are free, and the queue is empty.

At second j, the jth task is inserted into the queue (starting with the 0th task being inserted at second 0). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the smallest weight, and in case of a tie, it is assigned to a free server with the smallest index.

If there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned in order of insertion following the weight and index priorities above.

A server that is assigned task j at second t will be free again at second t + tasks[j].

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​​​​.

 

Example 1:

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:

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.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
    idle, busy = [], []
    for i, weight in enumerate(servers):
      heappush(idle, (weight, i))
    res = []
    for start, cost in enumerate(tasks):
      while busy and busy[0][0] <= start:
        _, s, i = heappop(busy)
        heappush(idle, (s, i))
      if idle:
        s, i = heappop(idle)
        heappush(busy, (start + cost, s, i))
      else:
        t, s, i = heappop(busy)
        heappush(busy, (t + cost, s, i))
      res.append(i)
    return res
class Solution {
  public int[] assignTasks(int[] servers, int[] tasks) {
    int m = tasks.length, n = servers.length;
    PriorityQueue<int[]> idle
      = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
    PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> {
      if (a[0] == b[0]) {
        return a[1] == b[1] ? a[2] - b[2] : a[1] - b[1];
      }
      return a[0] - b[0];
    });
    for (int i = 0; i < n; ++i) {
      idle.offer(new int[] {servers[i], i});
    }
    int[] res = new int[m];
    int j = 0;
    for (int start = 0; start < m; ++start) {
      int cost = tasks[start];
      while (!busy.isEmpty() && busy.peek()[0] <= start) {
        int[] item = busy.poll();
        idle.offer(new int[] {item[1], item[2]});
      }
      if (!idle.isEmpty()) {
        int[] item = idle.poll();
        res[j++] = item[1];
        busy.offer(new int[] {start + cost, item[0], item[1]});
      } else {
        int[] item = busy.poll();
        res[j++] = item[2];
        busy.offer(new int[] {item[0] + cost, item[1], item[2]});
      }
    }
    return res;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文