<?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>randomization Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/randomization/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/randomization/</link>
	<description></description>
	<lastBuildDate>Fri, 16 Mar 2018 11:55:03 +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>randomization Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/randomization/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
		<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-66394df0e7a4e175697664/] C++ [crayon-66394df0e7a51553170445/] &#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>
