<?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>quick selection Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/quick-selection/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/quick-selection/</link>
	<description></description>
	<lastBuildDate>Sat, 01 Jan 2022 00:20:32 +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>quick selection Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/quick-selection/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1985. Find the Kth Largest Integer in the Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1985-find-the-kth-largest-integer-in-the-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1985-find-the-kth-largest-integer-in-the-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 01 Jan 2022 00:11:48 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[nth_element]]></category>
		<category><![CDATA[quick selection]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9367</guid>

					<description><![CDATA[<p>You are given an array of strings&#160;nums&#160;and an integer&#160;k. Each string in&#160;nums&#160;represents an integer without leading zeros. Return&#160;the string that represents the&#160;kth&#160;largest integer&#160;in&#160;nums. Note: Duplicate&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1985-find-the-kth-largest-integer-in-the-array/">花花酱 LeetCode 1985. Find the Kth Largest Integer in the 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>You are given an array of strings&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>. Each string in&nbsp;<code>nums</code>&nbsp;represents an integer without leading zeros.</p>



<p>Return&nbsp;<em>the string that represents the&nbsp;</em><code>k<sup>th</sup></code><em><strong>&nbsp;largest integer</strong>&nbsp;in&nbsp;</em><code>nums</code>.</p>



<p><strong>Note</strong>: Duplicate numbers should be counted distinctly. For example, if&nbsp;<code>nums</code>&nbsp;is&nbsp;<code>["1","2","2"]</code>,&nbsp;<code>"2"</code>&nbsp;is the first largest integer,&nbsp;<code>"2"</code>&nbsp;is the second-largest integer, and&nbsp;<code>"1"</code>&nbsp;is the third-largest integer.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = ["3","6","7","10"], k = 4
<strong>Output:</strong> "3"
<strong>Explanation:</strong>
The numbers in nums sorted in non-decreasing order are ["3","6","7","10"].
The 4<sup>th</sup> largest integer in nums is "3".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = ["2","21","12","1"], k = 3
<strong>Output:</strong> "2"
<strong>Explanation:</strong>
The numbers in nums sorted in non-decreasing order are ["1","2","12","21"].
The 3<sup>rd</sup> largest integer in nums is "2".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = ["0","0"], k = 2
<strong>Output:</strong> "0"
<strong>Explanation:</strong>
The numbers in nums sorted in non-decreasing order are ["0","0"].
The 2<sup>nd</sup> largest integer in nums is "0".
</pre>



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



<ul><li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>4</sup></code></li><li><code>1 &lt;= nums[i].length &lt;= 100</code></li><li><code>nums[i]</code>&nbsp;consists of only digits.</li><li><code>nums[i]</code>&nbsp;will not have any leading zeros.</li></ul>



<h2><strong>Solution: nth_element / quick selection</strong></h2>



<p>Use <a rel="noreferrer noopener" href="https://en.cppreference.com/w/cpp/algorithm/nth_element" target="_blank">std::nth_element</a> to find the k-th largest element. When comparing two strings, compare their lengths first and compare their content if they have the same length.</p>



<p>Time complexity: O(n) on average<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:
  string kthLargestNumber(vector&lt;string&gt;&amp; nums, int k) {    
    nth_element(begin(nums), begin(nums) + k - 1, end(nums), [](const auto&amp; a, const auto&amp; b) {
      return a.length() == b.length() ? a &gt; b : a.length() &gt; b.length();      
    });
    return nums[k - 1];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1985-find-the-kth-largest-integer-in-the-array/">花花酱 LeetCode 1985. Find the Kth Largest Integer in the 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-1985-find-the-kth-largest-integer-in-the-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
