<?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>PDF Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pdf/</link>
	<description></description>
	<lastBuildDate>Sun, 29 Jul 2018 00:50:50 +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>PDF Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pdf/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
