<?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>shuffle Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/shuffle/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/shuffle/</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>shuffle Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/shuffle/</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 384. Shuffle an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-384-shuffle-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-384-shuffle-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 16 Mar 2018 10:36:19 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[permutation]]></category>
		<category><![CDATA[randomization]]></category>
		<category><![CDATA[shuffle]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2131</guid>

					<description><![CDATA[<p>Shuffle a set of numbers without duplicates. Example: [crayon-663a246881e29624511288/] C++ [crayon-663a246881e2c444823683/] &#160;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-384-shuffle-an-array/">花花酱 LeetCode 384. Shuffle an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Shuffle a set of numbers without duplicates.</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();

// Resets the array back to its original configuration [1,2,3].
solution.reset();

// Returns the random shuffling of array [1,2,3].
solution.shuffle();</pre><p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 249 ms
class Solution {
public:
  Solution(vector&lt;int&gt; nums) {
    nums_ = std::move(nums);
  }

  /** Resets the array to its original configuration and return it. */
  vector&lt;int&gt; reset() {
    return nums_;
  }

  /** Returns a random shuffling of the array. */
  vector&lt;int&gt; shuffle() {
    vector&lt;int&gt; output(nums_);
    const int n = nums_.size();
    for (int i = 0; i &lt; n - 1; ++i) {      
      int j = rand() % (n - i) + i;
      std::swap(output[i], output[j]);
    }
    return output;
  }
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-384-shuffle-an-array/">花花酱 LeetCode 384. Shuffle an Array</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-384-shuffle-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
