<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>schedule Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/schedule/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/schedule/</link>
	<description></description>
	<lastBuildDate>Mon, 09 Aug 2021 01:08:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>schedule Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/schedule/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1882. Process Tasks Using Servers</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1882-process-tasks-using-servers/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1882-process-tasks-using-servers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Aug 2021 01:07:18 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[schedule]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8527</guid>

					<description><![CDATA[<p>You are given two&#160;0-indexed&#160;integer arrays&#160;servers&#160;and&#160;tasks&#160;of lengths&#160;n​​​​​​ and&#160;m​​​​​​ respectively.&#160;servers[i]&#160;is the&#160;weight&#160;of the&#160;i​​​​​​th​​​​ server, and&#160;tasks[j]&#160;is the&#160;time needed&#160;to process the&#160;j​​​​​​th​​​​ task&#160;in seconds. Tasks are assigned to the servers using&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1882-process-tasks-using-servers/">花花酱 LeetCode 1882. Process Tasks Using Servers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>​​​​​​ and&nbsp;<code>m</code>​​​​​​ respectively.&nbsp;<code>servers[i]</code>&nbsp;is the&nbsp;<strong>weight</strong>&nbsp;of the&nbsp;<code>i<sup>​​​​​​th</sup></code>​​​​ server, and&nbsp;<code>tasks[j]</code>&nbsp;is the&nbsp;<strong>time needed</strong>&nbsp;to process the&nbsp;<code>j<sup>​​​​​​th</sup></code>​​​​ task&nbsp;<strong>in seconds</strong>.</p>



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



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



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



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



<p>Build an array&nbsp;<code>ans</code>​​​​ 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>​​​​​​th</sup></code>&nbsp;task will be assigned to.</p>



<p>Return&nbsp;<em>the array&nbsp;</em><code>ans</code>​​​​.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> servers = [3,3,2], tasks = [1,2,3,2,1,2]
<strong>Output:</strong> [2,2,0,2,1,2]
<strong>Explanation: </strong>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.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
<strong>Output:</strong> [1,4,1,4,1,3,2]
<strong>Explanation: </strong>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.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Simulation / Priority Queue</strong></h2>



<p>Two priority queues, one for free servers, another for releasing events.<br>One FIFO queue for tasks to schedule.</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; assignTasks(vector&lt;int&gt;&amp; servers, vector&lt;int&gt;&amp; tasks) {
    const int n = servers.size();
    const int m = tasks.size();
    using P = pair&lt;long, int&gt;;
    priority_queue&lt;P, vector&lt;P&gt;, greater&lt;P&gt;&gt; frees, release;
    for (int i = 0; i &lt; n; ++i)
      frees.emplace(servers[i], i);    
    vector&lt;int&gt; ans(m);
    queue&lt;int&gt; q;
    int l = 0;
    long t = 0;
    int count = 0;
    while (count != m) {      
      // Release servers.      
      while (!release.empty() &amp;&amp; release.top().first &lt;= t) {
        auto [rt, i] = release.top(); release.pop();
        frees.emplace(servers[i], i);        
      }
      // Enqueue tasks.
      while (l &lt; m &amp;&amp; l &lt;= t) q.push(l++);
      // Schedule tasks.
      while (q.size() &amp;&amp; frees.size()) {
        const int j = q.front(); q.pop();
        auto [w, i] = frees.top(); frees.pop();
        release.emplace(t + tasks[j], i);
        ans[j] = i;
        ++count;
      }
      // Advance time.
      if (frees.empty() &amp;&amp; !release.empty()) {             
        t = release.top().first;
      } else {
        ++t;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1882-process-tasks-using-servers/">花花酱 LeetCode 1882. Process Tasks Using Servers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-1882-process-tasks-using-servers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
