{"id":9367,"date":"2021-12-31T16:11:48","date_gmt":"2022-01-01T00:11:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9367"},"modified":"2021-12-31T16:20:32","modified_gmt":"2022-01-01T00:20:32","slug":"leetcode-1985-find-the-kth-largest-integer-in-the-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1985-find-the-kth-largest-integer-in-the-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1985. Find the Kth Largest Integer in the Array"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"3\",\"6\",\"7\",\"10\"], k = 4\n<strong>Output:<\/strong> \"3\"\n<strong>Explanation:<\/strong>\nThe numbers in nums sorted in non-decreasing order are [\"3\",\"6\",\"7\",\"10\"].\nThe 4<sup>th<\/sup> largest integer in nums is \"3\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"2\",\"21\",\"12\",\"1\"], k = 3\n<strong>Output:<\/strong> \"2\"\n<strong>Explanation:<\/strong>\nThe numbers in nums sorted in non-decreasing order are [\"1\",\"2\",\"12\",\"21\"].\nThe 3<sup>rd<\/sup> largest integer in nums is \"2\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"0\",\"0\"], k = 2\n<strong>Output:<\/strong> \"0\"\n<strong>Explanation:<\/strong>\nThe numbers in nums sorted in non-decreasing order are [\"0\",\"0\"].\nThe 2<sup>nd<\/sup> largest integer in nums is \"0\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: nth_element \/ quick selection<\/strong><\/h2>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n) on average<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string kthLargestNumber(vector<string>& nums, int k) {    \n    nth_element(begin(nums), begin(nums) + k - 1, end(nums), [](const auto& a, const auto& b) {\n      return a.length() == b.length() ? a > b : a.length() > b.length();      \n    });\n    return nums[k - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings&nbsp;nums&nbsp;and an integer&nbsp;k. Each string in&nbsp;nums&nbsp;represents an integer without leading zeros. Return&nbsp;the string that represents the&nbsp;kth&nbsp;largest integer&nbsp;in&nbsp;nums. Note: Duplicate&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,759,760,23],"class_list":["post-9367","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-nth_element","tag-quick-selection","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9367"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9367\/revisions"}],"predecessor-version":[{"id":9371,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9367\/revisions\/9371"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}