{"id":7448,"date":"2020-10-03T16:43:57","date_gmt":"2020-10-03T23:43:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7448"},"modified":"2020-10-03T18:04:30","modified_gmt":"2020-10-04T01:04:30","slug":"leetcode-1606-find-servers-that-handled-most-number-of-requests","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1606-find-servers-that-handled-most-number-of-requests\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1606. Find Servers That Handled Most Number of Requests"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1606. Find Servers That Handled Most Number of Requests - \u5237\u9898\u627e\u5de5\u4f5c EP359\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/9OqDLNyL8Fs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You have&nbsp;<code>k<\/code>&nbsp;servers numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>k-1<\/code>&nbsp;that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but&nbsp;<strong>cannot handle more than one request at a time<\/strong>. The requests are assigned to servers according to a specific algorithm:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;(0-indexed) request arrives.<\/li><li>If all servers are busy, the request is dropped (not handled at all).<\/li><li>If the&nbsp;<code>(i % k)<sup>th<\/sup><\/code>&nbsp;server is available, assign the request to that server.<\/li><li>Otherwise, assign the request to the next available server (wrapping around the list of servers and starting from 0 if necessary). For example, if the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;server is busy, try to assign the request to the&nbsp;<code>(i+1)<sup>th<\/sup><\/code>&nbsp;server, then the&nbsp;<code>(i+2)<sup>th<\/sup><\/code>&nbsp;server, and so on.<\/li><\/ul>\n\n\n\n<p>You are given a&nbsp;<strong>strictly increasing<\/strong>&nbsp;array&nbsp;<code>arrival<\/code>&nbsp;of positive integers, where&nbsp;<code>arrival[i]<\/code>&nbsp;represents the arrival time of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;request, and another array&nbsp;<code>load<\/code>, where&nbsp;<code>load[i]<\/code>&nbsp;represents the load of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;request (the time it takes to complete). Your goal is to find the&nbsp;<strong>busiest server(s)<\/strong>. A server is considered&nbsp;<strong>busiest<\/strong>&nbsp;if it handled the most number of requests successfully among all the servers.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list containing the IDs (0-indexed) of the&nbsp;<strong>busiest server(s)<\/strong><\/em>. You may return the IDs in any order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/08\/load-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3] \n<strong>Output:<\/strong> [1] \n<strong>Explanation:<\/strong>\nAll of the servers start out available.\nThe first 3 requests are handled by the first 3 servers in order.\nRequest 3 comes in. Server 0 is busy, so it's assigned to the next available server, which is 1.\nRequest 4 comes in. It cannot be handled since all servers are busy, so it is dropped.\nServers 0 and 2 handled one request each, while server 1 handled two requests. Hence server 1 is the busiest server.\n<\/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> k = 3, arrival = [1,2,3,4], load = [1,2,1,2]\n<strong>Output:<\/strong> [0]\n<strong>Explanation:<\/strong>\nThe first 3 requests are handled by first 3 servers.\nRequest 3 comes in. It is handled by server 0 since the server is available.\nServer 0 handled two requests, while servers 1 and 2 handled one request each. Hence server 0 is the busiest server.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> k = 3, arrival = [1,2,3], load = [10,12,11]\n<strong>Output:<\/strong> [0,1,2]\n<strong>Explanation: <\/strong>Each server handles a single request, so they are all considered the busiest.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> k = 3, arrival = [1,2,3,4,8,9,10], load = [5,2,10,3,1,2,2]\n<strong>Output:<\/strong> [1]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> k = 1, arrival = [1], load = [1]\n<strong>Output:<\/strong> [0]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= arrival.length, load.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>arrival.length == load.length<\/code><\/li><li><code>1 &lt;= arrival[i], load[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>arrival<\/code>&nbsp;is&nbsp;<strong>strictly increasing<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Heap + TreeSet<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1606-ep359-1.png\" alt=\"\" class=\"wp-image-7452\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1606-ep359-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1606-ep359-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1606-ep359-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Use a min heap to store the release time -> server.<br>Use a treeset to track the current available servers.<br>For reach request, check whether servers can be released at that time.<\/p>\n\n\n\n<p>Time complexity: O(nlogk)<br>Space complexity: O(k)<\/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> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n    priority_queue<pair<int, int>, vector<pair<int,int>>, greater<>> q; \/\/ {release_time, server}\n    set<int> servers;\n    vector<int> requests(k);\n    \n    for (int i = 0; i < k; ++i)\n      servers.insert(i);\n    \n    for (int i = 0; i < arrival.size(); ++i) {\n      const int t = arrival[i];\n      const int l = load[i];\n      \n      \/\/ Release servers. O(logk) per pop()\n      while (!q.empty() &#038;&#038; q.top().first <= t) {        \n        servers.insert(q.top().second);  \n        q.pop();\n      }\n      \n      \/\/ Drop the request.\n      if (servers.empty()) continue;\n      \n      \/\/ Find first avaiable one O(logk)\n      auto it = servers.lower_bound(i % k);\n      if (it == servers.end()) it = begin(servers);\n      const int idx = *it;\n      \n      ++requests[idx];\n      servers.erase(it);\n      q.emplace(t + l, idx);\n    }\n    const int max_req = *max_element(begin(requests), end(requests));\n    vector<int> ans;\n    for (int i = 0; i < k; ++i)\n      if (requests[i] == max_req) ans.push_back(i);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have&nbsp;k&nbsp;servers numbered from&nbsp;0&nbsp;to&nbsp;k-1&nbsp;that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but&nbsp;cannot handle more than one request at&#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":[73,382,179,656],"class_list":["post-7448","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-heap","tag-priority_queue","tag-simulation","tag-treeset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7448","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=7448"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7448\/revisions"}],"predecessor-version":[{"id":7453,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7448\/revisions\/7453"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}