{"id":9053,"date":"2021-12-05T20:06:14","date_gmt":"2021-12-06T04:06:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9053"},"modified":"2021-12-05T20:06:25","modified_gmt":"2021-12-06T04:06:25","slug":"leetcode-215-kth-largest-element-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-215-kth-largest-element-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 215. Kth Largest Element in an Array"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>, return&nbsp;<em>the<\/em>&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;<em>largest element in the array<\/em>.<\/p>\n\n\n\n<p>Note that it is the&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;largest element in the sorted order, not the&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;distinct element.<\/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,2,1,5,6,4], k = 2\n<strong>Output:<\/strong> 5\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 = [3,2,3,1,2,4,5,5,6], k = 4\n<strong>Output:<\/strong> 4\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>-10<sup>4<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Quick selection<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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  int findKthLargest(vector<int>& nums, int k) {\n    nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), std::greater<int>());\n    return nums[k - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k, return&nbsp;the&nbsp;kth&nbsp;largest element in the array. Note that it is the&nbsp;kth&nbsp;largest element in the sorted order, not the&nbsp;kth&nbsp;distinct element. Example&#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":[177,742,23],"class_list":["post-9053","post","type-post","status-publish","format-standard","hentry","category-array","tag-medium","tag-quick-select","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9053","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=9053"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9053\/revisions"}],"predecessor-version":[{"id":9055,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9053\/revisions\/9055"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}