<?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>min heap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/min-heap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/min-heap/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Apr 2021 17:18:12 +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>min heap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/min-heap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1834. Single-Threaded CPU</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1834-single-threaded-cpu/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1834-single-threaded-cpu/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Apr 2021 17:17:53 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[min heap]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8376</guid>

					<description><![CDATA[<p>You are given&#160;n​​​​​​ tasks labeled from&#160;0&#160;to&#160;n - 1&#160;represented by a 2D integer array&#160;tasks, where&#160;tasks[i] = [enqueueTimei, processingTimei]&#160;means that the&#160;i​​​​​​th​​​​ task will be available to process&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1834-single-threaded-cpu/">花花酱 LeetCode 1834. Single-Threaded CPU</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&nbsp;<code>n</code>​​​​​​ tasks labeled from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n - 1</code>&nbsp;represented by a 2D integer array&nbsp;<code>tasks</code>, where&nbsp;<code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code>&nbsp;means that the&nbsp;<code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at&nbsp;<code>enqueueTime<sub>i</sub></code>&nbsp;and will take&nbsp;<code>processingTime<sub>i</sub></code>to finish processing.</p>



<p>You have a single-threaded CPU that can process&nbsp;<strong>at most one</strong>&nbsp;task at a time and will act in the following way:</p>



<ul><li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li><li>If the CPU is idle and there are available tasks, the CPU will choose the one with the&nbsp;<strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li><li>Once a task is started, the CPU will&nbsp;<strong>process the entire task</strong>&nbsp;without stopping.</li><li>The CPU can finish a task then start a new one instantly.</li></ul>



<p>Return&nbsp;<em>the order in which the CPU will process the tasks.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]]
<strong>Output:</strong> [0,2,3,1]
<strong>Explanation: </strong>The events go as follows: 
- At time = 1, task 0 is available to process. Available tasks = {0}.
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
- At time = 2, task 1 is available to process. Available tasks = {1}.
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
- At time = 10, the CPU finishes task 1 and becomes idle.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
<strong>Output:</strong> [4,3,2,0,1]
<strong>Explanation</strong><strong>: </strong>The events go as follows:
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
- At time = 40, the CPU finishes task 1 and becomes idle.
</pre>



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



<ul><li><code>tasks.length == n</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Simulation w/ Sort + PQ</strong></h2>



<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; getOrder(vector&lt;vector&lt;int&gt;&gt;&amp; tasks) {
    const int n = tasks.size();
    for (int i = 0; i &lt; n; ++i)
      tasks[i].push_back(i);
    sort(begin(tasks), end(tasks)); // sort by enqueue_time;    
    
    vector&lt;int&gt; ans;
    priority_queue&lt;pair&lt;int, int&gt;&gt; q; // {-processing_time, -index}
    int i = 0;
    long t = 0;    
    while (ans.size() != n) {
      // Advance to next enqueue time if q is empty.
      if (i &lt; n &amp;&amp; q.empty() &amp;&amp; tasks[i][0] &gt; t)
        t = tasks[i][0];
      // Enqueue all available tasks.
      while (i &lt; n &amp;&amp; tasks[i][0] &lt;= t) {
        q.emplace(-tasks[i][1], -tasks[i][2]);
        ++i;
      }
      // Extra the top one.
      t -= q.top().first;
      ans.push_back(-q.top().second);
      q.pop();      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1834-single-threaded-cpu/">花花酱 LeetCode 1834. Single-Threaded CPU</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-1834-single-threaded-cpu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
