{"id":8527,"date":"2021-08-08T18:07:18","date_gmt":"2021-08-09T01:07:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8527"},"modified":"2021-08-08T18:08:00","modified_gmt":"2021-08-09T01:08:00","slug":"leetcode-1882-process-tasks-using-servers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1882-process-tasks-using-servers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1882. Process Tasks Using Servers"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays&nbsp;<code>servers<\/code>&nbsp;and&nbsp;<code>tasks<\/code>&nbsp;of lengths&nbsp;<code>n<\/code>\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;<code>m<\/code>\u200b\u200b\u200b\u200b\u200b\u200b respectively.&nbsp;<code>servers[i]<\/code>&nbsp;is the&nbsp;<strong>weight<\/strong>&nbsp;of the&nbsp;<code>i<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup><\/code>\u200b\u200b\u200b\u200b server, and&nbsp;<code>tasks[j]<\/code>&nbsp;is the&nbsp;<strong>time needed<\/strong>&nbsp;to process the&nbsp;<code>j<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup><\/code>\u200b\u200b\u200b\u200b task&nbsp;<strong>in seconds<\/strong>.<\/p>\n\n\n\n<p>Tasks are assigned to the servers using a&nbsp;<strong>task queue<\/strong>. Initially, all servers are free, and the queue is&nbsp;<strong>empty<\/strong>.<\/p>\n\n\n\n<p>At second&nbsp;<code>j<\/code>, the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;task is&nbsp;<strong>inserted<\/strong>&nbsp;into the queue (starting with the&nbsp;<code>0<sup>th<\/sup><\/code>&nbsp;task being inserted at second&nbsp;<code>0<\/code>). 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&nbsp;<strong>smallest weight<\/strong>, and in case of a tie, it is assigned to a free server with the&nbsp;<strong>smallest index<\/strong>.<\/p>\n\n\n\n<p>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&nbsp;<strong>in order of insertion<\/strong>&nbsp;following the weight and index priorities above.<\/p>\n\n\n\n<p>A server that is assigned task&nbsp;<code>j<\/code>&nbsp;at second&nbsp;<code>t<\/code>&nbsp;will be free again at second&nbsp;<code>t + tasks[j]<\/code>.<\/p>\n\n\n\n<p>Build an array&nbsp;<code>ans<\/code>\u200b\u200b\u200b\u200b of length&nbsp;<code>m<\/code>, where&nbsp;<code>ans[j]<\/code>&nbsp;is the&nbsp;<strong>index<\/strong>&nbsp;of the server the&nbsp;<code>j<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup><\/code>&nbsp;task will be assigned to.<\/p>\n\n\n\n<p>Return&nbsp;<em>the array&nbsp;<\/em><code>ans<\/code>\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> servers = [3,3,2], tasks = [1,2,3,2,1,2]\n<strong>Output:<\/strong> [2,2,0,2,1,2]\n<strong>Explanation: <\/strong>Events in chronological order go as follows:\n- At second 0, task 0 is added and processed using server 2 until second 1.\n- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.\n- At second 2, task 2 is added and processed using server 0 until second 5.\n- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.\n- At second 4, task 4 is added and processed using server 1 until second 5.\n- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]\n<strong>Output:<\/strong> [1,4,1,4,1,3,2]\n<strong>Explanation: <\/strong>Events in chronological order go as follows: \n- At second 0, task 0 is added and processed using server 1 until second 2.\n- At second 1, task 1 is added and processed using server 4 until second 2.\n- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4. \n- At second 3, task 3 is added and processed using server 4 until second 7.\n- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9. \n- At second 5, task 5 is added and processed using server 3 until second 7.\n- At second 6, task 6 is added and processed using server 2 until second 7.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>servers.length == n<\/code><\/li><li><code>tasks.length == m<\/code><\/li><li><code>1 &lt;= n, m &lt;= 2 * 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= servers[i], tasks[j] &lt;= 2 * 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation \/ Priority Queue<\/strong><\/h2>\n\n\n\n<p>Two priority queues, one for free servers, another for releasing events.<br>One FIFO queue for tasks to schedule.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> assignTasks(vector<int>& servers, vector<int>& tasks) {\n    const int n = servers.size();\n    const int m = tasks.size();\n    using P = pair<long, int>;\n    priority_queue<P, vector<P>, greater<P>> frees, release;\n    for (int i = 0; i < n; ++i)\n      frees.emplace(servers[i], i);    \n    vector<int> ans(m);\n    queue<int> q;\n    int l = 0;\n    long t = 0;\n    int count = 0;\n    while (count != m) {      \n      \/\/ Release servers.      \n      while (!release.empty() && release.top().first <= t) {\n        auto [rt, i] = release.top(); release.pop();\n        frees.emplace(servers[i], i);        \n      }\n      \/\/ Enqueue tasks.\n      while (l < m &#038;&#038; l <= t) q.push(l++);\n      \/\/ Schedule tasks.\n      while (q.size() &#038;&#038; frees.size()) {\n        const int j = q.front(); q.pop();\n        auto [w, i] = frees.top(); frees.pop();\n        release.emplace(t + tasks[j], i);\n        ans[j] = i;\n        ++count;\n      }\n      \/\/ Advance time.\n      if (frees.empty() &#038;&#038; !release.empty()) {             \n        t = release.top().first;\n      } else {\n        ++t;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;integer arrays&nbsp;servers&nbsp;and&nbsp;tasks&nbsp;of lengths&nbsp;n\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;m\u200b\u200b\u200b\u200b\u200b\u200b respectively.&nbsp;servers[i]&nbsp;is the&nbsp;weight&nbsp;of the&nbsp;i\u200b\u200b\u200b\u200b\u200b\u200bth\u200b\u200b\u200b\u200b server, and&nbsp;tasks[j]&nbsp;is the&nbsp;time needed&nbsp;to process the&nbsp;j\u200b\u200b\u200b\u200b\u200b\u200bth\u200b\u200b\u200b\u200b task&nbsp;in seconds. Tasks are assigned to the servers using&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,72,717,179],"class_list":["post-8527","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-priority-queue","tag-schedule","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8527"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8527\/revisions"}],"predecessor-version":[{"id":8529,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8527\/revisions\/8529"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}