<?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>quickselect Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/quickselect/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/quickselect/</link>
	<description></description>
	<lastBuildDate>Sun, 12 Dec 2021 18:36:59 +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>quickselect Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/quickselect/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2099. Find Subsequence of Length K With the Largest Sum</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2099-find-subsequence-of-length-k-with-the-largest-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2099-find-subsequence-of-length-k-with-the-largest-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Dec 2021 18:32:39 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[k-th]]></category>
		<category><![CDATA[n-th element]]></category>
		<category><![CDATA[quickselect]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9130</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;and an integer&#160;k. You want to find a&#160;subsequence&#160;of&#160;nums&#160;of length&#160;k&#160;that has the&#160;largest&#160;sum. Return&#160;any&#160;such subsequence as an integer array of length&#160;k. A&#160;subsequence&#160;is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2099-find-subsequence-of-length-k-with-the-largest-sum/">花花酱 LeetCode 2099. Find Subsequence of Length K With the Largest Sum</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>You are given an integer array&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>. You want to find a&nbsp;<strong>subsequence&nbsp;</strong>of&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>k</code>&nbsp;that has the&nbsp;<strong>largest</strong>&nbsp;sum.</p>



<p>Return<em>&nbsp;</em><em><strong>any</strong>&nbsp;such subsequence as an integer array of length&nbsp;</em><code>k</code>.</p>



<p>A&nbsp;<strong>subsequence</strong>&nbsp;is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1,3,3], k = 2
<strong>Output:</strong> [3,3]
<strong>Explanation:</strong>
The subsequence has the largest sum of 3 + 3 = 6.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,-2,3,4], k = 3
<strong>Output:</strong> [-1,3,4]
<strong>Explanation:</strong> 
The subsequence has the largest sum of -1 + 3 + 4 = 6.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,3,3], k = 2
<strong>Output:</strong> [3,4]
<strong>Explanation:</strong>
The subsequence has the largest sum of 3 + 4 = 7. 
Another possible subsequence is [4, 3].
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>-10<sup>5</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= k &lt;= nums.length</code></li></ul>



<h2><strong>Solution 1: Full sorting + Hashtable</strong></h2>



<p>Make a copy of the original array, sort it in whole and put k largest elements in a hashtable.</p>



<p>Scan the original array and append the number to the answer array if it&#8217;s in the hashtable.</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; maxSubsequence(vector&lt;int&gt;&amp; nums, int k) {    
    vector&lt;int&gt; ans;
    vector&lt;int&gt; s(nums);
    sort(rbegin(s), rend(s));
    unordered_map&lt;int, int&gt; m;
    for (int i = 0; i &lt; k; ++i) ++m[s[i]];
    for (int x : nums) 
      if (--m[x] &gt;= 0) 
        ans.push_back(x);
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: k-th element</strong></h2>



<p>Use quick select / partial sort to find the k-th largest element of the array. Any number greater than that must be in the sequence. The tricky part is: what about the pivot itself? We couldn&#8217;t include &#8217;em all blindly. Need to figure out how many copies of the pivot is there in the k largest and only include as many as of that in the final answer.</p>



<p>Time complexity: O(n+k)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; maxSubsequence(vector&lt;int&gt;&amp; nums, int k) {    
    vector&lt;int&gt; ans;
    vector&lt;int&gt; s(nums);
    nth_element(begin(s), begin(s) + k - 1, end(s), greater&lt;int&gt;());
    const int pivot = s[k - 1];
    // How many pivots in the largest k elements.
    int left = count(begin(s), begin(s) + k, pivot);    
    for (size_t i = 0; i != nums.size(); ++i)
      if (nums[i] &gt; pivot || (nums[i] == pivot &amp;&amp; --left &gt;= 0)) 
        ans.push_back(nums[i]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2099-find-subsequence-of-length-k-with-the-largest-sum/">花花酱 LeetCode 2099. Find Subsequence of Length K With the Largest Sum</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-2099-find-subsequence-of-length-k-with-the-largest-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1471. The k Strongest Values in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1471-the-k-strongest-values-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1471-the-k-strongest-values-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 Jun 2020 05:30:36 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[quickselect]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6883</guid>

					<description><![CDATA[<p>Given an array of integers&#160;arr&#160;and an integer&#160;k. A value&#160;arr[i]&#160;is said to be stronger than a value&#160;arr[j]&#160;if&#160;&#124;arr[i] - m&#124; &#62; &#124;arr[j]&#160;- m&#124;&#160;where&#160;m&#160;is the&#160;median&#160;of the array.If&#160;&#124;arr[i] -&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1471-the-k-strongest-values-in-an-array/">花花酱 LeetCode 1471. The k Strongest Values in 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>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer&nbsp;<code>k</code>.</p>



<p>A value&nbsp;<code>arr[i]</code>&nbsp;is said to be stronger than a value&nbsp;<code>arr[j]</code>&nbsp;if&nbsp;<code>|arr[i] - m| &gt; |arr[j]&nbsp;- m|</code>&nbsp;where&nbsp;<code>m</code>&nbsp;is the&nbsp;<strong>median</strong>&nbsp;of the array.<br>If&nbsp;<code>|arr[i] - m| == |arr[j] - m|</code>, then&nbsp;<code>arr[i]</code>&nbsp;is said to be stronger than&nbsp;<code>arr[j]</code>&nbsp;if&nbsp;<code>arr[i] &gt; arr[j]</code>.</p>



<p>Return&nbsp;<em>a list of the strongest&nbsp;<code>k</code></em>&nbsp;values in the array. return the answer&nbsp;<strong>in any arbitrary order</strong>.</p>



<p><strong>Median</strong>&nbsp;is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position&nbsp;<code>((n - 1) / 2)</code>&nbsp;in the sorted list&nbsp;<strong>(0-indexed)</strong>.</p>



<ul><li>For&nbsp;<code>arr =&nbsp;[6, -3, 7, 2, 11]</code>,&nbsp;<code>n = 5</code>&nbsp;and the median is obtained by sorting the array&nbsp;<code>arr = [-3, 2, 6, 7, 11]</code>&nbsp;and the median is&nbsp;<code>arr[m]</code>&nbsp;where&nbsp;<code>m = ((5 - 1) / 2) = 2</code>. The median is&nbsp;<code>6</code>.</li><li>For&nbsp;<code>arr =&nbsp;[-7, 22, 17, 3]</code>,&nbsp;<code>n = 4</code>&nbsp;and the median is obtained by sorting the array&nbsp;<code>arr = [-7, 3, 17, 22]</code>&nbsp;and the median is&nbsp;<code>arr[m]</code>&nbsp;where&nbsp;<code>m = ((4 - 1) / 2) = 1</code>. The median is&nbsp;<code>3</code>.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3,4,5], k = 2
<strong>Output:</strong> [5,1]
<strong>Explanation:</strong> Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also <strong>accepted</strong> answer.
Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 &gt; 1.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,1,3,5,5], k = 2
<strong>Output:</strong> [5,5]
<strong>Explanation:</strong> Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [6,7,11,7,6,8], k = 5
<strong>Output:</strong> [11,8,6,6,7]
<strong>Explanation:</strong> Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].
Any permutation of [11,8,6,6,7] is <strong>accepted</strong>.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [6,-3,7,2,11], k = 3
<strong>Output:</strong> [-3,11,2]
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [-7,22,17,3], k = 2
<strong>Output:</strong> [22,17]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= arr.length &lt;= 10^5</code></li><li><code>-10^5 &lt;= arr[i] &lt;= 10^5</code></li><li><code>1 &lt;= k &lt;= arr.length</code></li></ul>



<h2><strong>Solution 1: quick selection + sort</strong></h2>



<p>Step 1: find the median element m<br>Step 2: Sort the array according to m<br>Step 3: output the first k elements of the sorted array.</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getStrongest(vector&lt;int&gt;&amp; arr, int k) {
    const int n = arr.size();
    const int i = (n - 1) / 2;
    // sort(begin(arr), end(arr));
    nth_element(begin(arr), begin(arr) + i, end(arr));
    const int m = arr[i];  
    sort(begin(arr), end(arr), [&amp;](const int x, const int y) {      
      return abs(x - m) != abs(y - m) ? abs(x - m) &gt; abs(y - m) : x &gt; y;      
    });    
    return {begin(arr), begin(arr) + k};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1471-the-k-strongest-values-in-an-array/">花花酱 LeetCode 1471. The k Strongest Values in 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-1471-the-k-strongest-values-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1387. Sort Integers by The Power Value</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1387-sort-integers-by-the-power-value/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1387-sort-integers-by-the-power-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 19:57:18 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[quickselect]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6530</guid>

					<description><![CDATA[<p>The power of an integer&#160;x&#160;is defined as the number of steps needed to transform&#160;x&#160;into&#160;1&#160;using the following steps: if&#160;x&#160;is even then&#160;x = x / 2 if&#160;x&#160;is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1387-sort-integers-by-the-power-value/">花花酱 LeetCode 1387. Sort Integers by The Power Value</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>The power of an integer&nbsp;<code>x</code>&nbsp;is defined as the number of steps needed to transform&nbsp;<code>x</code>&nbsp;into&nbsp;<code>1</code>&nbsp;using the following steps:</p>



<ul><li>if&nbsp;<code>x</code>&nbsp;is even then&nbsp;<code>x = x / 2</code></li><li>if&nbsp;<code>x</code>&nbsp;is odd then&nbsp;<code>x = 3 * x + 1</code></li></ul>



<p>For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 &#8211;&gt; 10 &#8211;&gt; 5 &#8211;&gt; 16 &#8211;&gt; 8 &#8211;&gt; 4 &#8211;&gt; 2 &#8211;&gt; 1).</p>



<p>Given three integers&nbsp;<code>lo</code>,&nbsp;<code>hi</code>&nbsp;and&nbsp;<code>k</code>. The task is to sort all integers in the interval&nbsp;<code>[lo, hi]</code>&nbsp;by the power value in&nbsp;<strong>ascending order</strong>, if two or more integers have&nbsp;<strong>the same</strong>&nbsp;power value sort them by&nbsp;<strong>ascending order</strong>.</p>



<p>Return the&nbsp;<code>k-th</code>&nbsp;integer in the range&nbsp;<code>[lo, hi]</code>&nbsp;sorted by the power value.</p>



<p>Notice that for any&nbsp;integer&nbsp;<code>x</code>&nbsp;<code>(lo &lt;= x &lt;= hi)</code>&nbsp;it is&nbsp;<strong>guaranteed</strong>&nbsp;that&nbsp;<code>x</code>&nbsp;will transform into&nbsp;<code>1</code>&nbsp;using these steps and that the power of&nbsp;<code>x</code>&nbsp;is will&nbsp;<strong>fit</strong>&nbsp;in 32 bit signed integer.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lo = 12, hi = 15, k = 2
<strong>Output:</strong> 13
<strong>Explanation:</strong> The power of 12 is 9 (12 --&gt; 6 --&gt; 3 --&gt; 10 --&gt; 5 --&gt; 16 --&gt; 8 --&gt; 4 --&gt; 2 --&gt; 1)
The power of 13 is 9
The power of 14 is 17
The power of 15 is 17
The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.
Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lo = 1, hi = 1, k = 1
<strong>Output:</strong> 1
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lo = 7, hi = 11, k = 4
<strong>Output:</strong> 7
<strong>Explanation:</strong> The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].
The interval sorted by power is [8, 10, 11, 7, 9].
The fourth number in the sorted array is 7.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lo = 10, hi = 20, k = 5
<strong>Output:</strong> 13
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> lo = 1, hi = 1000, k = 777
<strong>Output:</strong> 570
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= lo &lt;= hi &lt;= 1000</code></li><li><code>1 &lt;= k &lt;= hi - lo + 1</code></li></ul>



<h2><strong>Solution: Precompute + quick select</strong></h2>



<p>Time complexity: O(nlogn) + O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int getKth(int lo, int hi, int k) {
    vector&lt;pair&lt;int, int&gt;&gt; vals;
    for (int n = lo; n &lt;= hi; ++n) {
      int p = 0;
      int x = n;
      while (x != 1) {
        if (x &amp; 1) x = 3 * x + 1;
        else x /= 2;
        ++p;
      }
      vals.emplace_back(p, n);
    }
    nth_element(begin(vals), begin(vals) + k - 1, end(vals));
    return vals[k - 1].second;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1387-sort-integers-by-the-power-value/">花花酱 LeetCode 1387. Sort Integers by The Power Value</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/simulation/leetcode-1387-sort-integers-by-the-power-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
