<?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>pick Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pick/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pick/</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>pick Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pick/</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>
		<item>
		<title>花花酱 LeetCode 880. Random Pick with Weight</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Jul 2018 00:08:09 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[CDF]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[pick]]></category>
		<category><![CDATA[random]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3339</guid>

					<description><![CDATA[<p>Problem Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1 &#60;= w.length &#60;= 10000&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/">花花酱 LeetCode 880. Random Pick with Weight</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 an array <code>w</code> of positive integers, where <code>w[i]</code> describes the weight of index <code>i</code>, write a function <code>pickIndex</code> which randomly picks an index in proportion to its weight.</p>
<p>Note:</p>
<ol>
<li><code>1 &lt;= w.length &lt;= 10000</code></li>
<li><code>1 &lt;= w[i] &lt;= 10^5</code></li>
<li><code>pickIndex</code> will be called at most <code>10000</code> times.</li>
</ol>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: 
</strong><span id="example-input-1-1">["Solution","pickIndex"]
</span><span id="example-input-1-2">[[[1]],[]]</span>
<strong>Output: </strong><span id="example-output-1">[null,0]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: 
</strong><span id="example-input-2-1">["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
</span><span id="example-input-2-2">[[[1,3]],[],[],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-2">[null,0,1,1,1,0]</span></pre>
</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 <code>w</code>. <code>pickIndex</code> has no arguments. Arguments are always wrapped with a list, even if there aren&#8217;t any.</p>
<h1><strong>Solution: Binary Search</strong></h1>
<ol>
<li>Convert PDF to CDF</li>
<li>Uniformly sample a value s in [1, sum(weights)].</li>
<li>Use binary search to find first index such that PDF[index] &gt;= s.</li>
</ol>
<p>Time complexity: Init O(n), query O(logn)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 184 ms
class Solution {
public:
  Solution(vector&lt;int&gt; w) { 
    sums_ = std::move(w);
    for (int i = 1; i &lt; sums_.size(); ++i)
      sums_[i] += sums_[i - 1];
  }

  int pickIndex() {
    int s = rand() / static_cast&lt;double&gt;(RAND_MAX) * sums_.back() + 1;
    return lower_bound(sums_.begin(), sums_.end(), s) - sums_.begin();
    // or
    // int l = 0;
    // int r = sums_.size();
    // while (l &lt; r) {
    //   int m = (r - l) / 2 + l;
    //   if (sums_[m] &lt; s)
    //     l = m + 1;
    //   else
    //     r = m;
    // }
    // return l;
  }
private:  
  vector&lt;int&gt; sums_;
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/geometry/leetcode-882-random-point-in-non-overlapping-rectangles/">花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-880-random-pick-with-weight/">花花酱 LeetCode 880. Random Pick with Weight</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/math/leetcode-880-random-pick-with-weight/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 398. Random Pick Index</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-398-random-pick-index/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-398-random-pick-index/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 16 Mar 2018 11:52:26 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[pick]]></category>
		<category><![CDATA[randomization]]></category>
		<category><![CDATA[sample]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2138</guid>

					<description><![CDATA[<p>Problem: https://leetcode.com/problems/random-pick-index/description/ Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-398-random-pick-index/">花花酱 LeetCode 398. Random Pick Index</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>Problem:</h1>
<p><a href="https://leetcode.com/problems/random-pick-index/description/">https://leetcode.com/problems/random-pick-index/description/</a></p>
<p>Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.</p>
<p><b>Note:</b><br />
The array size can be very large. Solution that uses too much extra space will not pass the judge.</p>
<h2><b>Example:</b></h2>
<p></p><pre class="crayon-plain-tag">int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);

// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);

// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);</pre><p></p>
<h1>Solution: Reservoir sampling</h1>
<p><a href="https://en.wikipedia.org/wiki/Reservoir_sampling">https://en.wikipedia.org/wiki/Reservoir_sampling</a></p>
<p>Time complexity: O(query * n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 92 ms
class Solution {
public:
  Solution(vector&lt;int&gt; nums) {
    nums_ = std::move(nums);
  }

  int pick(int target) {
    int n = 0;    
    int index = -1;
    for (int i = 0; i &lt; nums_.size(); ++i) {
      if (nums_[i] != target) continue;
      ++n;
      if (rand() % n == 0) index = i;
    }
    return index;
  }
private:
  vector&lt;int&gt; nums_;
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-398-random-pick-index/">花花酱 LeetCode 398. Random Pick Index</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/array/leetcode-398-random-pick-index/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
