<?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>weighted Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/weighted/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/weighted/</link>
	<description></description>
	<lastBuildDate>Sun, 29 Jul 2018 00:52:58 +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>weighted Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/weighted/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Jul 2018 00:45:20 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[pick]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[weighted]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3342</guid>

					<description><![CDATA[<p>Problem Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles. Note: An integer point is a point&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/">花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a list of <strong>non-overlapping</strong> axis-aligned rectangles <code>rects</code>, write a function <code>pick</code> which randomly and uniformily picks an <strong>integer point</strong> in the space covered by the rectangles.</p>
<p>Note:</p>
<ol>
<li>An <strong>integer point</strong> is a point that has integer coordinates.</li>
<li>A point on the perimeter of a rectangle is <strong>included</strong> in the space covered by the rectangles.</li>
<li><code>i</code>th rectangle = <code>rects[i]</code> = <code>[x1,y1,x2,y2]</code>, where <code>[x1, y1]</code> are the integer coordinates of the bottom-left corner, and <code>[x2, y2]</code> are the integer coordinates of the top-right corner.</li>
<li>length and width of each rectangle does not exceed <code>2000</code>.</li>
<li><code>1 &lt;= rects.length &lt;= 100</code></li>
<li><code>pick</code> return a point as an array of integer coordinates <code>[p_x, p_y]</code></li>
<li><code>pick</code> is called at most <code>10000</code> times.</li>
</ol>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: 
</strong><span id="example-input-1-1">["Solution","pick","pick","pick"]
</span><span id="example-input-1-2">[[[[1,1,5,5]]],[],[],[]]</span>
<strong>Output: 
</strong><span id="example-output-1">[null,[4,1],[4,1],[3,3]]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: 
</strong><span id="example-input-2-1">["Solution","pick","pick","pick","pick","pick"]
</span><span id="example-input-2-2">[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]</span>
<strong>Output: 
</strong><span id="example-output-2">[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]</span></pre>
</div>
<div>
<p><strong>Explanation of Input Syntax:</strong></p>
<p>The input is two lists: the subroutines called and their arguments. <code>Solution</code>&#8216;s constructor has one argument, the array of rectangles <code>rects</code>. <code>pick</code> has no arguments. Arguments are always wrapped with a list, even if there aren&#8217;t any.</p>
</div>
<h1><strong>Solution: Binary Search</strong></h1>
<p>Same as <a href="http://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/">LeetCode 880. Random Pick with Weight</a></p>
<p>Use area of the rectangles as weights.</p>
<p>Time complexity: Init: O(n) Pick: O(logn)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 92 ms
class Solution {
public:
  Solution(vector&lt;vector&lt;int&gt;&gt; rects): rects_(std::move(rects)) {
    sums_ = vector&lt;int&gt;(rects_.size());
    for (int i = 0; i &lt; rects_.size(); ++i) {
      sums_[i] = (rects_[i][2] - rects_[i][0] + 1) * (rects_[i][3] - rects_[i][1] + 1);
      if (i &gt; 0) sums_[i] += sums_[i - 1];
    }
  }

  vector&lt;int&gt; pick() {
    const int s = nextInt(sums_.back()) + 1;
    int index = lower_bound(sums_.begin(), sums_.end(), s) - sums_.begin();
    const auto&amp; rect = rects_[index];
    return {rect[0] + nextInt(rect[2] - rect[0] + 1),
            rect[1] + nextInt(rect[3] - rect[1] + 1)};
  }
    
private:  
  vector&lt;int&gt; sums_;
  vector&lt;vector&lt;int&gt;&gt; rects_;
  
  // Returns a random int in [0, n - 1]
  int nextInt(int n) {
    return rand() / static_cast&lt;double&gt;(RAND_MAX) * n;
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/">花花酱 LeetCode 880. Random Pick with Weight</a></li>
</ul>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/">花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles</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/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
