<?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>k-th Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/k-th/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/k-th/</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>k-th Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/k-th/</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 1738. Find Kth Largest XOR Coordinate Value</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1738-find-kth-largest-xor-coordinate-value/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1738-find-kth-largest-xor-coordinate-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 06:23:11 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[k-th]]></category>
		<category><![CDATA[submatrix]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8029</guid>

					<description><![CDATA[<p>You are given a 2D&#160;matrix&#160;of size&#160;m x n, consisting of non-negative integers. You are also given an integer&#160;k. The&#160;value&#160;of coordinate&#160;(a, b)&#160;of the matrix is the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1738-find-kth-largest-xor-coordinate-value/">花花酱 LeetCode 1738. Find Kth Largest XOR Coordinate 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>You are given a 2D&nbsp;<code>matrix</code>&nbsp;of size&nbsp;<code>m x n</code>, consisting of non-negative integers. You are also given an integer&nbsp;<code>k</code>.</p>



<p>The&nbsp;<strong>value</strong>&nbsp;of coordinate&nbsp;<code>(a, b)</code>&nbsp;of the matrix is the XOR of all&nbsp;<code>matrix[i][j]</code>&nbsp;where&nbsp;<code>0 &lt;= i &lt;= a &lt; m</code>&nbsp;and&nbsp;<code>0 &lt;= j &lt;= b &lt; n</code>&nbsp;<strong>(0-indexed)</strong>.</p>



<p>Find the&nbsp;<code>k<sup>th</sup></code>&nbsp;largest value&nbsp;<strong>(1-indexed)</strong>&nbsp;of all the coordinates of&nbsp;<code>matrix</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[5,2],[1,6]], k = 1
<strong>Output:</strong> 7
<strong>Explanation:</strong> The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[5,2],[1,6]], k = 2
<strong>Output:</strong> 5
<strong>Explanation: </strong>The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[5,2],[1,6]], k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[5,2],[1,6]], k = 4
<strong>Output:</strong> 0
<strong>Explanation:</strong> The value of coordinate (1,1) is 5 XOR 2 XOR 1 XOR 6 = 0, which is the 4th largest value.</pre>



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



<ul><li><code>m == matrix.length</code></li><li><code>n == matrix[i].length</code></li><li><code>1 &lt;= m, n &lt;= 1000</code></li><li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6</sup></code></li><li><code>1 &lt;= k &lt;= m * n</code></li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>Similar to <a href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-304-range-sum-query-2d-immutable/" data-type="post" data-id="348">花花酱 LeetCode 304. Range Sum Query 2D – Immutable</a></p>



<p>xor[i][j] = matrix[i][j] ^ xor[i &#8211; 1][j &#8211; 1] ^ xor[i &#8211; 1][j] ^ xor[i][j- 1]</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int kthLargestValue(vector&lt;vector&lt;int&gt;&gt;&amp; matrix, int k) {    
    const int m = matrix.size(), n = matrix[0].size();
    vector&lt;int&gt; v;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        v.push_back(matrix[i][j] 
                      ^= (i ? matrix[i - 1][j] : 0) 
                       ^ (j ? matrix[i][j - 1] : 0) 
                       ^ (i * j ? matrix[i - 1][j - 1] : 0));
    nth_element(begin(v), begin(v) + k - 1, end(v), greater&lt;int&gt;());    
    return v[k - 1];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1738-find-kth-largest-xor-coordinate-value/">花花酱 LeetCode 1738. Find Kth Largest XOR Coordinate 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/dynamic-programming/leetcode-1738-find-kth-largest-xor-coordinate-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
