{"id":149412,"date":"2020-08-24T05:45:11","date_gmt":"2020-08-23T21:45:11","guid":{"rendered":"http:\/\/4563.org\/?p=149412"},"modified":"2020-08-24T05:45:11","modified_gmt":"2020-08-23T21:45:11","slug":"lintcode-leetcode-%e9%a2%98%e8%a7%a3-%e7%8b%97%e7%8b%97%e5%ae%b6%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e7%ac%ac-k-%e5%a4%a7%e5%85%83%e7%b4%a0%ef%bc%88%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f%e6%b3%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149412","title":{"rendered":"[Lintcode\/Leetcode \u9898\u89e3] \u72d7\u72d7\u5bb6\u9762\u8bd5\u9898\uff1a\u7b2c k \u5927\u5143\u7d20\uff08\u5feb\u901f\u6392\u5e8f\u6cd5\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [Lintcode\/Leetcode \u9898\u89e3] \u72d7\u72d7\u5bb6\u9762\u8bd5\u9898\uff1a\u7b2c k \u5927\u5143\u7d20\uff08\u5feb\u901f\u6392\u5e8f\u6cd5\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728\u6570\u7ec4\u4e2d\u627e\u5230\u7b2c k \u5927\u7684\u5143\u7d20\u3002(\u4f60\u53ef\u4ee5\u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7684\u4f4d\u7f6e)<\/p>\n<p>\u70b9\u6b64\u5728\u7ebf\u505a\u9898<\/p>\n<h2>\u6837\u4f8b 1\uff1a<\/h2>\n<pre><code>\u8f93\u5165\uff1a n = 1, nums = [1,3,4,2] \u8f93\u51fa\uff1a 4  <\/code><\/pre>\n<h2>\u6837\u4f8b 2\uff1a<\/h2>\n<pre><code>\u8f93\u5165\uff1a n = 3, nums = [9,3,2,4,8] \u8f93\u51fa\uff1a 4  <\/code><\/pre>\n<h1>[\u9898\u89e3]<\/h1>\n<p> <\/p>\n<h2>\u7b97\u6cd5\uff1a\u5feb\u901f\u9009\u62e9\u7b97\u6cd5<\/h2>\n<p>\u6700\u5bb9\u6613\u60f3\u5230\u7684\u5c31\u662f\u76f4\u63a5\u6392\u5e8f\uff0c\u8fd4\u56de\u7b2c k \u5927\u7684\u503c\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(nlogn)\uff0c\u8fd9\u91cc\u63d0\u4f9b O(n)\u7684\u89e3\u6cd5\u3002<\/p>\n<p>\u8fd9\u9898\u5176\u5b9e\u662f\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u53d8\u4f53\uff0c\u5728\u4e5d\u7ae0\u7b97\u6cd5\u73ed\u4e2d\u4e5f\u6709\u8be6\u7ec6\u8bb2\u89e3\u3002\u901a\u8fc7\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684 partition \u6b65\u9aa4\uff0c\u53ef\u4ee5\u5c06\u5c0f\u4e8e pivot \u7684\u503c\u5212\u5206\u5230 pivot \u5de6\u8fb9\uff0c\u5927\u4e8e pivot \u7684\u503c\u5212\u5206\u5230 pivot \u53f3\u8fb9\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230 pivot \u7684 rank \u3002\u4ece\u800c\u7f29\u5c0f\u8303\u56f4\u7ee7\u7eed\u627e\u7b2c k \u5927\u7684\u503c\u3002<\/p>\n<p>partition \u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u4ee4 left = start\uff0cright = end\uff0cpivot = nums[left]\u3002<\/li>\n<li>\u5f53 nums[left] &lt; pivot \u65f6\uff0cleft \u6307\u9488\u5411\u53f3\u79fb\u52a8\u3002<\/li>\n<li>\u5f53 nums[right] &gt; pivot \u65f6\uff0cright \u6307\u9488\u5411\u5de6\u79fb\u52a8\u3002<\/li>\n<li>\u4ea4\u6362\u4e24\u4e2a\u4f4d\u7f6e\u7684\u503c\uff0cright \u6307\u9488\u5de6\u79fb\uff0cleft \u6307\u9488\u53f3\u79fb\u3002<\/li>\n<li>\u76f4\u5230\u4e24\u6307\u9488\u76f8\u9047\uff0c\u5426\u5219\u56de\u5230\u7b2c 2 \u6b65\u3002 \u6bcf\u6b21 partition \u540e\u6839\u636e pivot \u7684\u4f4d\u7f6e\uff0c\u5bfb\u627e\u4e0b\u4e00\u4e2a\u641c\u7d22\u7684\u8303\u56f4\u3002<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<p>\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a n<\/p>\n<h2>\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/h2>\n<ul>\n<li>\u5bf9\u4e00\u4e2a\u6570\u7ec4\u8fdb\u884c partition \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/li>\n<li>\u5206\u6cbb\uff0c\u9009\u62e9\u4e00\u8fb9\u7ee7\u7eed\u8fdb\u884c partition \u3002<\/li>\n<li>\u6240\u4ee5\u603b\u7684\u590d\u6742\u5ea6\u4e3a T(n) = T(n \/ 2) + O(n)\uff0c\u603b\u65f6\u95f4\u590d\u6742\u5ea6\u4f9d\u7136\u4e3a O(n)\u3002<\/li>\n<\/ul>\n<h2>\u7a7a\u95f4\u590d\u6742\u5ea6 O(1)<\/h2>\n<p>\u53ea\u9700\u8981\u5feb\u901f\u9009\u62e9\u6e38\u6807\u7684 O(1)\u989d\u5916\u7a7a\u95f4\u3002<\/p>\n<pre><code>public class Solution {     \/**      * @param n: An integer      * @param nums: An array      * @return: the Kth largest element      *\/     public int kthLargestElement(int k, int[] nums) {         int n = nums.length;         \/\/ \u4e3a\u4e86\u65b9\u4fbf\u7f16\u5199\u4ee3\u7801\uff0c\u8fd9\u91cc\u5c06\u7b2c k \u5927\u8f6c\u6362\u6210\u7b2c k \u5c0f\u95ee\u9898\u3002         k = n - k;         return partition(nums, 0, n - 1, k);     }     public int partition(int[] nums, int start, int end, int k) {         int left = start, right = end;         int pivot = nums[left];          while (left &lt;= right) {             while (left &lt;= right &amp;&amp; nums[left] &lt; pivot) {                 left++;             }             while (left &lt;= right &amp;&amp; nums[right] &gt; pivot) {                 right--;             }             if (left &lt;= right) {                 swap(nums, left, right);                 left++;                 right--;             }         }          \/\/ \u5982\u679c\u7b2c k \u5c0f\u5728\u53f3\u4fa7\uff0c\u641c\u7d22\u53f3\u8fb9\u7684\u8303\u56f4\uff0c\u5426\u5219\u641c\u7d22\u5de6\u4fa7\u3002         if (k &lt;= right) {             return partition(nums, start, right, k);         }         if (k &gt;= left) {             return partition(nums, left, end, k);         }         return nums[k];     }     public void swap(int[] nums, int x, int y) {         int temp = nums[x];         nums[x] = nums[y];         nums[y] = temp;     } }  <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u89c1<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[Lintcode\/Leetcod&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149412"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149412"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149412\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149412"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}