<?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>sample Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sample/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sample/</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jul 2018 15:36:28 +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>sample Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sample/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 881. Random Flip Matrix</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-881-random-flip-matrix/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-881-random-flip-matrix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 31 Jul 2018 15:33:05 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[shuffle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3398</guid>

					<description><![CDATA[<p>Problem You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all values are initially 0. Write a function flip which chooses a 0 value uniformly at random, changes&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-881-random-flip-matrix/">花花酱 LeetCode 881. Random Flip Matrix</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>You are given the number of rows <code>n_rows</code> and number of columns <code>n_cols</code> of a 2D binary matrix where all values are initially 0. Write a function <code>flip</code> which chooses a 0 value <a href="https://en.wikipedia.org/wiki/Discrete_uniform_distribution" target="_blank" rel="noopener">uniformly at random</a>, changes it to 1, and then returns the position <code>[row.id, col.id]</code> of that value. Also, write a function <code>reset</code> which sets all values back to 0. <strong>Try to minimize the number of calls to system&#8217;s Math.random()</strong> and optimize the time and space complexity.</p>
<p>Note:</p>
<ol>
<li><code>1 &lt;= n_rows, n_cols &lt;= 10000</code></li>
<li><code>0 &lt;= row.id &lt; n_rows</code> and <code>0 &lt;= col.id &lt; n_cols</code></li>
<li><code>flip</code> will not be called when the matrix has no 0 values left.</li>
<li>the total number of calls to <code>flip</code> and <code>reset</code> will not exceed 1000.</li>
</ol>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: 
</strong><span id="example-input-1-1">["Solution","flip","flip","flip","flip"]
</span><span id="example-input-1-2">[[2,3],[],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-1">[null,[0,1],[1,2],[1,0],[1,1]]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: 
</strong><span id="example-input-2-1">["Solution","flip","flip","reset","flip"]
</span><span id="example-input-2-2">[[1,2],[],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-2">[null,[0,0],[0,1],null,[0,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 two arguments, <code>n_rows</code> and <code>n_cols</code>. <code>flip</code> and <code>reset</code> have no arguments. Arguments are always wrapped with a list, even if there aren&#8217;t any.</p>
<h1><strong>Solution 1: Hashtable + Resample</strong></h1>
<p>Time complexity: O(|flip|) = O(1000) = O(1)</p>
<p>Space complexity: O(|flip|) = O(1000) = O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
  Solution(int n_rows, int n_cols): 
    rows_(n_rows), cols_(n_cols), n_(rows_ * cols_) {}

  vector&lt;int&gt; flip() {
    int index = rand() % n_;
    while (m_.count(index))
      index = rand() % n_;    
    m_.insert(index);
    return {index / cols_, index % cols_};
  }

  void reset() {
    m_.clear();
    n_ = rows_ * cols_;
  }
private:
  const int rows_;
  const int cols_;
  int n_;
  unordered_set&lt;int&gt; m_;
};</pre><p></p>
<h1><strong>Solution 2: </strong><a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher–Yates shuffle</a></h1>
<p>Generate a random shuffle of 0 to n &#8211; 1, one number at a time.</p>
<p>Time complexity: flip: O(1)</p>
<p>Space complexity: O(|flip|) = O(1000) = O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
  Solution(int n_rows, int n_cols): 
    rows_(n_rows), cols_(n_cols), n_(rows_ * cols_) {}

  vector&lt;int&gt; flip() {
    int s = rand() % (n_--);
    int index = s;
    if (m_.count(s))
      index = m_[s];
    m_[s] = m_.count(n_) ? m_[n_] : n_;
    return {index / cols_, index % cols_};
  }

  void reset() {
    m_.clear();
    n_ = rows_ * cols_;
  }
private:
  const int rows_;
  const int cols_;
  int n_;
  unordered_map&lt;int, int&gt; m_;
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-881-random-flip-matrix/">花花酱 LeetCode 881. Random Flip Matrix</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-881-random-flip-matrix/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>
