<?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>offline processing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/offline-processing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/offline-processing/</link>
	<description></description>
	<lastBuildDate>Sun, 02 May 2021 20:17:09 +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>offline processing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/offline-processing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1847. Closest Room</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1847-closest-room/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1847-closest-room/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 01 May 2021 23:01:00 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[offline processing]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8419</guid>

					<description><![CDATA[<p>There is a hotel with&#160;n&#160;rooms. The rooms are represented by a 2D integer array&#160;rooms&#160;where&#160;rooms[i] = [roomIdi, sizei]&#160;denotes that there is a room with room number&#160;roomIdi&#160;and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1847-closest-room/">花花酱 LeetCode 1847. Closest Room</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1847. Closest Room - 刷题找工作 EP393" width="500" height="281" src="https://www.youtube.com/embed/JQTV3yzSBZE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>There is a hotel with&nbsp;<code>n</code>&nbsp;rooms. The rooms are represented by a 2D integer array&nbsp;<code>rooms</code>&nbsp;where&nbsp;<code>rooms[i] = [roomId<sub>i</sub>, size<sub>i</sub>]</code>&nbsp;denotes that there is a room with room number&nbsp;<code>roomId<sub>i</sub></code>&nbsp;and size equal to&nbsp;<code>size<sub>i</sub></code>. Each&nbsp;<code>roomId<sub>i</sub></code>&nbsp;is guaranteed to be&nbsp;<strong>unique</strong>.</p>



<p>You are also given&nbsp;<code>k</code>&nbsp;queries in a 2D array&nbsp;<code>queries</code>&nbsp;where&nbsp;<code>queries[j] = [preferred<sub>j</sub>, minSize<sub>j</sub>]</code>. The answer to the&nbsp;<code>j<sup>th</sup></code>&nbsp;query is the room number&nbsp;<code>id</code>&nbsp;of a room such that:</p>



<ul><li>The room has a size of&nbsp;<strong>at least</strong>&nbsp;<code>minSize<sub>j</sub></code>, and</li><li><code>abs(id - preferred<sub>j</sub>)</code>&nbsp;is&nbsp;<strong>minimized</strong>, where&nbsp;<code>abs(x)</code>&nbsp;is the absolute value of&nbsp;<code>x</code>.</li></ul>



<p>If there is a&nbsp;<strong>tie</strong>&nbsp;in the absolute difference, then use the room with the&nbsp;<strong>smallest</strong>&nbsp;such&nbsp;<code>id</code>. If there is&nbsp;<strong>no such room</strong>, the answer is&nbsp;<code>-1</code>.</p>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>&nbsp;of length&nbsp;</em><code>k</code><em>&nbsp;where&nbsp;</em><code>answer[j]</code><em>&nbsp;contains the answer to the&nbsp;</em><code>j<sup>th</sup></code><em>&nbsp;query</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
<strong>Output:</strong> [3,-1,3]
<strong>Explanation: </strong>The answers to the queries are as follows:
Query = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3.
Query = [3,3]: There are no rooms with a size of at least 3, so the answer is -1.
Query = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. The answer is 3.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]
<strong>Output:</strong> [2,1,3]
<strong>Explanation: </strong>The answers to the queries are as follows:
Query = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2.
Query = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller.
Query = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.</pre>



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



<ul><li><code>n == rooms.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>k == queries.length</code></li><li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li><li><code>1 &lt;= roomId<sub>i</sub>, preferred<sub>j</sub>&nbsp;&lt;= 10<sup>7</sup></code></li><li><code>1 &lt;= size<sub>i</sub>, minSize<sub>j</sub>&nbsp;&lt;= 10<sup>7</sup></code></li></ul>



<h2><strong>Solution: Off Processing: Sort + Binary Search</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-1.png" alt="" class="wp-image-8423" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-2.png" alt="" class="wp-image-8424" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



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



<p>Sort queries and rooms by size in descending order, only add valid rooms (size &gt;= min_size) to the treeset for binary search.</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; closestRoom(vector&lt;vector&lt;int&gt;&gt;&amp; rooms, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    const int n = rooms.size();
    const int m = queries.size();
    for (int i = 0; i &lt; m; ++i)
      queries[i].push_back(i);
    // Sort queries by size DESC.
    sort(begin(queries), end(queries), [](const auto&amp; q1, const auto&amp; q2){
      return q1[1] &gt; q2[1];
    });
    // Sort room by size DESC.
    sort(begin(rooms), end(rooms), [](const auto&amp; r1, const auto&amp; r2) {
      return r1[1] &gt; r2[1];
    });
    vector&lt;int&gt; ans(m); 
    int j = 0;
    set&lt;int&gt; ids;
    for (const auto&amp; q : queries) {
      while (j &lt; n &amp;&amp; rooms[j][1] &gt;= q[1])
        ids.insert(rooms[j++][0]);      
      if (ids.empty()) {
        ans[q[2]] = -1;
        continue;
      }
      const int id = q[0];
      auto it = ids.lower_bound(id);
      int id1 = (it != end(ids)) ? *it : INT_MAX;
      int id2 = id1;
      if (it != begin(ids))
        id2 = *prev(it);
      ans[q[2]] = abs(id1 - id) &lt; abs(id2 - id) ? id1 : id2;
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Fenwick Tree</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-3.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-3.png" alt="" class="wp-image-8425" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/05/1847-ep393-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Time complexity: O(nlogS + mlogSlogn)<br>Space complexity: O(nlogS)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; closestRoom(vector&lt;vector&lt;int&gt;&gt;&amp; rooms,
                          vector&lt;vector&lt;int&gt;&gt;&amp; queries) {    
    S = 0;
    for (const auto&amp; r : rooms) S = max(S, r[1]); 
    for (const auto&amp; r : rooms) update(r[1], r[0]);    
    vector&lt;int&gt; ans;
    for (const auto&amp; q : queries)
      ans.push_back(query(q[1], q[0]));
    return ans;
  }
private:
  int S;
  void update(int s, int id) {
    for (s = S - s + 1; s &lt;= S; s += s &amp; (-s))
      tree[s].insert(id);
  }
  
  int query(int s, int id) {
    int ans = -1;
    for (s = S - s + 1; s &gt; 0; s -= s &amp; (-s)) {
      if (!tree.count(s)) continue;
      int id1 = INT_MAX;      
      int id2 = INT_MAX;
      auto it = tree[s].lower_bound(id);
      if (it != end(tree[s])) id1 = *it;      
      if (it != begin(tree[s])) id2 = *prev(it);      
      int cid = (abs(id1 - id) &lt; abs(id2 - id)) ? id1 : id2;
      if (ans == -1 || abs(cid - id) &lt; abs(ans - id))
        ans = cid;
      else if (abs(cid - id) == abs(ans - id))
        ans = min(ans, cid);
    }
    return ans;
  }
  unordered_map&lt;int, set&lt;int&gt;&gt; tree;
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1847-closest-room/">花花酱 LeetCode 1847. Closest Room</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/algorithms/binary-search/leetcode-1847-closest-room/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
