<?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>events Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/events/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/events/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 13:55:46 +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>events Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/events/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1942. The Number of the Smallest Unoccupied Chair</title>
		<link>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1942-the-number-of-the-smallest-unoccupied-chair/</link>
					<comments>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1942-the-number-of-the-smallest-unoccupied-chair/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 13:50:08 +0000</pubDate>
				<category><![CDATA[Priority Queue]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[treeset]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9308</guid>

					<description><![CDATA[<p>There is a party where&#160;n&#160;friends numbered from&#160;0&#160;to&#160;n - 1&#160;are attending. There is an&#160;infinite&#160;number of chairs in this party that are numbered from&#160;0&#160;to&#160;infinity. When a friend&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1942-the-number-of-the-smallest-unoccupied-chair/">花花酱 LeetCode 1942. The Number of the Smallest Unoccupied Chair</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>There is a party where&nbsp;<code>n</code>&nbsp;friends numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n - 1</code>&nbsp;are attending. There is an&nbsp;<strong>infinite</strong>&nbsp;number of chairs in this party that are numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>infinity</code>. When a friend arrives at the party, they sit on the unoccupied chair with the&nbsp;<strong>smallest number</strong>.</p>



<ul><li>For example, if chairs&nbsp;<code>0</code>,&nbsp;<code>1</code>, and&nbsp;<code>5</code>&nbsp;are occupied when a friend comes, they will sit on chair number&nbsp;<code>2</code>.</li></ul>



<p>When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.</p>



<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;2D integer array&nbsp;<code>times</code>&nbsp;where&nbsp;<code>times[i] = [arrival<sub>i</sub>, leaving<sub>i</sub>]</code>, indicating the arrival and leaving times of the&nbsp;<code>i<sup>th</sup></code>&nbsp;friend respectively, and an integer&nbsp;<code>targetFriend</code>. All arrival times are&nbsp;<strong>distinct</strong>.</p>



<p>Return<em>&nbsp;the&nbsp;<strong>chair number</strong>&nbsp;that the friend numbered&nbsp;</em><code>targetFriend</code><em>&nbsp;will sit on</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> times = [[1,4],[2,3],[4,6]], targetFriend = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 
- Friend 0 arrives at time 1 and sits on chair 0.
- Friend 1 arrives at time 2 and sits on chair 1.
- Friend 1 leaves at time 3 and chair 1 becomes empty.
- Friend 0 leaves at time 4 and chair 0 becomes empty.
- Friend 2 arrives at time 4 and sits on chair 0.
Since friend 1 sat on chair 1, we return 1.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> times = [[3,10],[1,5],[2,6]], targetFriend = 0
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
- Friend 1 arrives at time 1 and sits on chair 0.
- Friend 2 arrives at time 2 and sits on chair 1.
- Friend 0 arrives at time 3 and sits on chair 2.
- Friend 1 leaves at time 5 and chair 0 becomes empty.
- Friend 2 leaves at time 6 and chair 1 becomes empty.
- Friend 0 leaves at time 10 and chair 2 becomes empty.
Since friend 0 sat on chair 2, we return 2.
</pre>



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



<ul><li><code>n == times.length</code></li><li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li><li><code>times[i].length == 2</code></li><li><code>1 &lt;= arrival<sub>i</sub>&nbsp;&lt; leaving<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= targetFriend &lt;= n - 1</code></li><li>Each&nbsp;<code>arrival<sub>i</sub></code>&nbsp;time is&nbsp;<strong>distinct</strong>.</li></ul>



<h2><strong>Solution: Treeset + Simulation</strong></h2>



<p>Use a treeset to track available chairs, sort events by time.<br>note: process leaving events first.</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:
  int smallestChair(vector&lt;vector&lt;int&gt;&gt;&amp; times, int targetFriend) {
    const int n = times.size();
    vector&lt;pair&lt;int, int&gt;&gt; arrival(n);
    vector&lt;pair&lt;int, int&gt;&gt; leaving(n);
    for (int i = 0; i &lt; n; ++i) {
      arrival[i] = {times[i][0], i};
      leaving[i] = {times[i][1], i};
    }
    vector&lt;int&gt; pos(n);
    iota(begin(pos), end(pos), 0); // pos = [0, 1, ..., n -1]
    set&lt;int&gt; aval(begin(pos), end(pos));    
    sort(begin(arrival), end(arrival));
    sort(begin(leaving), end(leaving));
    for (int i = 0, j = 0; i &lt; n; ++i) {
      const auto [t, u] = arrival[i];
      while (j &lt; n &amp;&amp; leaving[j].first &lt;= t)        
        aval.insert(pos[leaving[j++].second]);        
      aval.erase(pos[u] = *begin(aval));      
      if (u == targetFriend) return pos[u];
    }
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1942-the-number-of-the-smallest-unoccupied-chair/">花花酱 LeetCode 1942. The Number of the Smallest Unoccupied Chair</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/priority-queue/leetcode-1942-the-number-of-the-smallest-unoccupied-chair/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2054. Two Best Non-Overlapping Events</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Nov 2021 02:16:37 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8653</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;2D integer array of&#160;events&#160;where&#160;events[i] = [startTimei, endTimei, valuei]. The&#160;ith&#160;event starts at&#160;startTimeiand ends at&#160;endTimei, and if you attend this event, you will receive&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/">花花酱 LeetCode 2054. Two Best Non-Overlapping Events</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 a&nbsp;<strong>0-indexed</strong>&nbsp;2D integer array of&nbsp;<code>events</code>&nbsp;where&nbsp;<code>events[i] = [startTime<sub>i</sub>, endTime<sub>i</sub>, value<sub>i</sub>]</code>. The&nbsp;<code>i<sup>th</sup></code>&nbsp;event starts at&nbsp;<code>startTime<sub>i</sub></code>and ends at&nbsp;<code>endTime<sub>i</sub></code>, and if you attend this event, you will receive a value of&nbsp;<code>value<sub>i</sub></code>. You can choose&nbsp;<strong>at most</strong>&nbsp;<strong>two</strong>&nbsp;<strong>non-overlapping</strong>&nbsp;events to attend such that the sum of their values is&nbsp;<strong>maximized</strong>.</p>



<p>Return&nbsp;<em>this&nbsp;<strong>maximum</strong>&nbsp;sum.</em></p>



<p>Note that the start time and end time is&nbsp;<strong>inclusive</strong>: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time&nbsp;<code>t</code>, the next event must start at or after&nbsp;<code>t + 1</code>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture5.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,3,2],[4,5,2],[2,4,3]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture1.png" alt="Example 1 Diagram"/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,3,2],[4,5,2],[1,5,5]]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Choose event 2 for a sum of 5.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,5,3],[1,5,1],[6,6,5]]
<strong>Output:</strong> 8
<strong>Explanation: </strong>Choose events 0 and 2 for a sum of 3 + 5 = 8.</pre>



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



<ul><li><code>2 &lt;= events.length &lt;= 10<sup>5</sup></code></li><li><code>events[i].length == 3</code></li><li><code>1 &lt;= startTime<sub>i</sub>&nbsp;&lt;= endTime<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= value<sub>i</sub>&nbsp;&lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Sort + Heap</strong></h2>



<p>Sort events by start time, process them from left to right.</p>



<p>Use a min heap to store the events processed so far, a variable cur to track the max value of a non-overlapping event.</p>



<p>For a given event, pop all non-overlapping events whose end time is smaller than its start time and update cur.</p>



<p>ans = max(val + cur)</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:
  int maxTwoEvents(vector&lt;vector&lt;int&gt;&gt;&amp; events) {
    using E = pair&lt;int,int&gt;;
    sort(begin(events), end(events));
    priority_queue&lt;E, vector&lt;E&gt;, greater&lt;E&gt;&gt; q; // (end_time, val)
    int ans = 0;
    int cur = 0;
    for (const auto&amp; e : events) {
      while (!q.empty() &amp;&amp; q.top().first &lt; e[0]) {
        cur = max(cur, q.top().second);
        q.pop();
      }
      ans = max(ans, cur + e[2]);
      q.emplace(e[1], e[2]);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/">花花酱 LeetCode 2054. Two Best Non-Overlapping Events</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/heap/leetcode-2054-two-best-non-overlapping-events/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
