{"id":397302,"date":"2021-03-21T20:06:25","date_gmt":"2021-03-21T12:06:25","guid":{"rendered":"http:\/\/4563.org\/?p=397302"},"modified":"2021-03-21T20:06:25","modified_gmt":"2021-03-21T12:06:25","slug":"leetcode-lintcode-%e9%a2%98%e8%a7%a3-%e5%ad%97%e8%8a%82%e8%b7%b3%e5%8a%a8%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%90%88%e5%b9%b6-k-%e4%b8%aa%e6%8e%92%e5%ba%8f%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=397302","title":{"rendered":"[LeetCode\/LintCode \u9898\u89e3] \u5b57\u8282\u8df3\u52a8\u9762\u8bd5\u9898\uff1a\u5408\u5e76 k \u4e2a\u6392\u5e8f\u6570\u7ec4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [LeetCode\/LintCode \u9898\u89e3] \u5b57\u8282\u8df3\u52a8\u9762\u8bd5\u9898\uff1a\u5408\u5e76 k \u4e2a\u6392\u5e8f\u6570\u7ec4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5c06 k \u4e2a\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u5927\u7684\u6709\u5e8f\u6570\u7ec4\u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1:<\/h3>\n<pre><code>Input:    [     [1, 3, 5, 7],     [2, 4, 6],     [0, 8, 9, 10, 11]   ] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <\/code><\/pre>\n<h3>\u6837\u4f8b 2:<\/h3>\n<pre><code>Input:   [     [1,2,3],     [1,2]   ] Output: [1,1,2,2,3] <\/code><\/pre>\n<h2>\u7b97\u6cd5\u4e00 \u66b4\u529b<\/h2>\n<p>\u9898\u76ee\u6307\u51fa k \u4e2a\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u501f\u9274\u5f52\u5e76\u6392\u5e8f\u7684\u601d\u60f3 \u6bcf\u6b21\u904d\u5386 k \u4e2a\u6709\u5e8f\u6570\u7ec4\u7684\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u627e\u51fa\u6700\u5c0f\u7684\u90a3\u4e2a\uff0c\u7136\u540e\u538b\u5165\u7b54\u6848 ans \u6570\u7ec4\uff0c\u8bb0\u5f97\u628a\u6700\u5c0f\u7684\u90a3\u4e2a\u4ece\u5b83\u539f\u5148\u7684\u6570\u7ec4\u7ed9\u5220\u9664 \u6bcf\u6b21\u627e\u6700\u5c0f\u7684\u662f O(K)\u7684\uff0c\u6240\u4ee5\u603b\u590d\u6742\u5ea6\u662f O(NK)\u7684\uff0cN \u662f k \u4e2a\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u7684\u603b\u6570\u91cf<\/p>\n<h2>\u7b97\u6cd5\u4e8c \u4f18\u5148\u961f\u5217\u4f18\u5316<\/h2>\n<p>\u6839\u636e\u7b97\u6cd5\u4e00\uff0c\u6765\u8fdb\u884c\u4f18\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6709\u5e8f\u96c6\u5408\u6765\u627e\u6700\u5c0f\u503c\uff0c\u6bd4\u5982 set map \u5806 \u5e73\u8861\u6811\u4e00\u7c7b\u90fd\u53ef\u4ee5\uff0c\u6211\u4eec\u8fd9\u91cc\u7528\u5806\u6765\u52a0\u901f\u6c42\u6700\u5c0f\u503c\u7684\u64cd\u4f5c \u4f18\u5148\u961f\u5217<\/p>\n<ul>\n<li>\u5148\u5c06\u6bcf\u4e2a\u6709\u5e8f\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u538b\u5165\u4f18\u5148\u961f\u5217\u4e2d<\/li>\n<li>\u4e0d\u505c\u7684\u4ece\u4f18\u5148\u961f\u5217\u4e2d\u53d6\u51fa\u6700\u5c0f\u5143\u7d20\uff08\u4e5f\u5c31\u662f\u5806\u9876\uff09\uff0c\u518d\u5c06\u8fd9\u4e2a\u6700\u5c0f\u5143\u7d20\u6240\u5728\u7684\u6709\u5e8f\u6570\u7ec4\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u538b\u5165\u961f\u5217\u4e2d eg. \u6700\u5c0f\u5143\u7d20\u4e3a x\uff0c\u5b83\u662f\u7b2c j \u4e2a\u6570\u7ec4\u7684\u7b2c p \u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u7b2c j \u4e2a\u6570\u7ec4\u7684\u7b2c p+1 \u4e2a\u5143\u7d20\u538b\u5165\u961f\u5217<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<h3>\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u56e0\u4e3a\u4e00\u5f00\u59cb\u961f\u5217\u91cc\u9762\u6700\u591a k \u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u6bcf\u6b21\u53d6\u51fa\u4e00\u4e2a\u5143\u7d20\uff0c\u6709\u53ef\u80fd\u518d\u538b\u5165\u4e00\u4e2a\u65b0\u5143\u7d20\uff0c\u6240\u4ee5\u961f\u5217\u5143\u7d20\u6570\u91cf\u7684\u4e0a\u9650\u5c31\u662f K\uff0c\u6240\u4ee5\u6211\u4eec\u6bcf\u6b21\u538b\u5165\u5143\u7d20\u548c\u53d6\u51fa\u5143\u7d20\u90fd\u662f logK \u7684\uff0c\u56e0\u4e3a\u8981\u628a k \u4e2a\u6570\u7ec4\u90fd\u6392\u5e8f\u5b8c\u6210\uff0c\u90a3\u4e48\u6240\u6709\u5143\u7d20\u90fd\u4f1a\u5165\u961f \u518d\u51fa\u961f\u4e00\u6b21\uff0c\u6240\u4ee5\u603b\u5171\u590d\u6742\u5ea6\u662f$(NlogK) N \u662f K \u4e2a\u6570\u7ec4\u91cc\u9762\u6240\u6709\u5143\u7d20\u7684\u6570\u91cf<\/p>\n<h3>\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u5f00\u8f9f\u7684\u5806\u7684\u7a7a\u95f4\u662f O(K)\u7684\uff0c\u8f93\u5165\u7684\u7a7a\u95f4\u662f O(N),\u603b\u7a7a\u95f4\u590d\u6742\u5ea6 O(N+K)<\/p>\n<pre><code> public class Solution {     \/**      * @param arrays: k sorted integer arrays      * @return: a sorted array      *\/     static class Node implements Comparator&lt;Node&gt; {         public int value;         public int arrayIdx;         public int idx;          public Node() {          }         \/\/value \u6743\u503c\u5927\u5c0f\uff0carraysIdx \u5728\u54ea\u4e2a\u6570\u7ec4\u91cc\uff0cidx \u5728\u8be5\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e&gt; &gt;         public Node(int value, int arrayIdx, int idx) {             this.value = value;             this.arrayIdx = arrayIdx;             this.idx = idx;         }          public int compare(Node n1, Node n2) {             if(n1.value &lt; n2.value) {                 return 1;             } else {                 return 0;             }         }     }     static Comparator&lt;Node&gt; cNode = new Comparator&lt;Node&gt;() {         public int compare(Node o1, Node o2) {             return o1.value - o2.value;         }      };     public int[] mergekSortedArrays(int[][] arrays) {          \/\/ \u521d\u59cb\u5316 \u4f18\u5148\u961f\u5217 \uff0c\u6211\u4eec\u4f18\u5148\u961f\u5217\u7684\u4e00\u4e2a\u5143\u7d20\u5305\u62ec\u4e09\u4e2a\u503c \uff1a\u6570\u5b57\u5927\u5c0f\uff0c\u6570\u5b57\u5728\u54ea\u4e2a\u6570\u7ec4\u91cc\uff0c\u6570\u5b57\u5728\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e         PriorityQueue&lt;Node&gt; q = new PriorityQueue&lt;Node&gt;(arrays.length + 5, cNode);         \/\/ \u521d\u59cb\u5316 \u7b54\u6848         List&lt;Integer&gt; ans = new ArrayList&lt;&gt;();          for(int i = 0; i &lt; arrays.length; i++) {             \/\/ \u5982\u679c\u8fd9\u4e2a\u6570\u7ec4\u4e3a\u7a7a \u5219\u4e0d\u7528\u538b\u5165             if(arrays[i].length == 0) {                 continue;             }             \/\/ arrays[i][0] \u6743\u503c\u5927\u5c0f  i \u5728\u7b2c i \u4e2a\u6570\u7ec4   0 \u5728\u8be5\u6570\u7ec4\u7684 0 \u4f4d\u7f6e             q.add(new Node(arrays[i][0], i, 0));         }         while(!q.isEmpty()) {             \/\/ \u53d6\u51fa\u961f\u5217\u4e2d\u6700\u5c0f\u503c             Node point = q.poll();              \/\/ \u6743\u503c \uff0c\u6240\u5728\u6570\u7ec4\u7684\u7f16\u53f7\uff0c\u5728\u8be5\u6570\u7ec4\u7684\u4f4d\u7f6e\u7f16\u53f7             int value = point.value;             int arrayIdx = point.arrayIdx;             int idx = point.idx;              \/\/  \u66f4\u65b0\u7b54\u6848\u6570\u7ec4             ans.add(value);                \/\/ \u5b83\u5df2\u7ecf\u662f\u6240\u5728\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4e86\uff0c\u8fd9\u4e2a\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u90fd\u5df2\u7ecf\u5904\u7406\u5b8c\u6bd5             if(idx == arrays[arrayIdx].length - 1) {                 continue;             } else {                 \/\/ \u538b\u5165\u5b83\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u65b0\u5143\u7d20                 Node newPoint = new Node(arrays[arrayIdx][idx + 1], arrayIdx, idx + 1);                 q.add(newPoint);             }         }         return ans.stream().mapToInt(Integer::valueOf).toArray();     }  } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/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>[LeetCode\/LintCod&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\/397302"}],"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=397302"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/397302\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397302"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}