{"id":1017,"date":"2025-12-23T08:30:00","date_gmt":"2025-12-23T00:30:00","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1017"},"modified":"2025-12-23T08:30:00","modified_gmt":"2025-12-23T00:30:00","slug":"leetcode-215-%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%ac%ack%e4%b8%aa%e6%9c%80%e5%a4%a7%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1017","title":{"rendered":"LeetCode 215 \u2013 \u6570\u7ec4\u4e2d\u7684\u7b2cK\u4e2a\u6700\u5927\u5143\u7d20"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u6574\u6570\u6570\u7ec4 nums \u548c\u6574\u6570 k\uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u7b2c k \u4e2a\u6700\u5927\u7684\u5143\u7d20\u3002<br \/>\n\u8bf7\u6ce8\u610f\uff0c\u4f60\u9700\u8981\u627e\u7684\u662f\u6570\u7ec4\u6392\u5e8f\u540e\u7684\u7b2c k \u4e2a\u6700\u5927\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u7b2c k \u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002<br \/>\n\u4f60\u5fc5\u987b\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n) \u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165: [3,2,1,5,6,4], k = 2<br \/>\n\u8f93\u51fa: 5<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u4f18\u5148\u961f\u5217<\/h2>\n<p>\u904d\u5386\u6570\u7ec4\uff0c\u653e\u5165\u5c0f\u9876\u4f18\u5148\u961f\u5217\uff0c\u8d85\u51fa k \u4e2a\u65f6\u53d6\u51fa1\u4e2a\u9876\u90e8\u5143\u7d20\u3002<br \/>\n\u6700\u7ec8\u9876\u90e8\u5143\u7d20\u5373\u4e3a\u7b2cK\u4e2a\u6700\u5927\u5143\u7d20\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n * logk)<\/p>\n<h2>\u54c8\u5e0c\u8ba1\u6570<\/h2>\n<p>\u904d\u5386\u4e00\u8fb9\u6570\u7ec4\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u8bb0\u5f55\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u540c\u65f6\u627e\u51fa\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<br \/>\n\u4ece\u6700\u5927\u503c\u5f00\u59cb\u5bf9\u6b21\u6570\u6c42\u548c\uff0csum >= k \u65f6\u9000\u51fa\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/p>\n<h2>\u5feb\u901f\u9009\u62e9<\/h2>\n<p>\u5feb\u6392\u7684\u8fc7\u7a0b\u4e2d\u5224\u65ad k \u843d\u5728\u54ea\u4e2a\u533a\u95f4\uff0c\u7136\u540e\u9012\u5f52.<br \/>\n\u6bcf\u5c42\u5e73\u5747\u51cf\u5c11\u4e00\u534a\u5143\u7d20\uff0c\u5904\u7406\u6b21\u6570\u4e3a n + n\/2 + n\/4 &#8230; + n\/x &lt;= 2n<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int findKthLargest(int[] nums, int k) {\n    PriorityQueue&lt;Integer&gt; queue = new PriorityQueue&lt;&gt;();\n    for (int x : nums) {\n        queue.offer(x);\n        if (queue.size() &gt; k) {\n            queue.poll();\n        }\n    }\n    return queue.poll();\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">\/\/ \u4f18\u5148\u961f\u5217\nfun findKthLargest(nums: IntArray, k: Int): Int {\n    val queue = PriorityQueue&lt;Int&gt;()\n    for (n in nums) {\n        queue.offer(n)\n        if (queue.size &gt; k) {\n            queue.poll()\n        }\n    }\n    return queue.poll()\n}\n\n\/\/ \u54c8\u5e0c\u8868\u8ba1\u6570\nfun findKthLargest2(nums: IntArray, k: Int): Int {\n    var min = nums.first()\n    var max = nums.first()\n    val count = HashMap&lt;Int, Int&gt;()\n    for (n in nums) {\n        count[n] = (count[n] ?: 0) + 1\n        min = min(min, n)\n        max = max(max, n)\n    }\n    var sum = 0\n    for (n in max downTo min) {\n        sum += count[n] ?: 0\n        if (sum &gt;= k) {\n            return n\n        }\n    }\n    return 0\n}\n\n\n\/\/ \u5feb\u901f\u9009\u62e9\nfun findKthLargest(nums: IntArray, k: Int, start: Int = 0, end: Int = nums.size - 1): Int {\n    val target = nums[end]\n    var index = start\n    for (i in start..end) {\n        if (nums[i] &gt; target) {\n            swap(nums, i, index++)\n        }\n    }\n    swap(nums, end, index)\n    val count = index - start \/\/ \u5de6\u4fa7\u6570\u91cf\n\n    return if (count &lt; k - 1) {\n        findKthLargest(nums, k - count - 1, index + 1, end)\n    } else if (count &gt; k - 1) {\n        findKthLargest(nums, k, start, index - 1)\n    } else {\n        nums[index]\n    }\n}\n\nprivate fun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u6574\u6570\u6570\u7ec4 nums \u548c\u6574\u6570 k\uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u7b2c k \u4e2a\u6700\u5927\u7684\u5143\u7d20\u3002 \u8bf7\u6ce8\u610f\uff0c\u4f60\u9700\u8981\u627e\u7684\u662f\u6570\u7ec4\u6392\u5e8f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,79,74,51],"class_list":["post-1017","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-79","tag-74","tag-51"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1017"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1018,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions\/1018"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}