{"id":161841,"date":"2020-09-23T13:41:53","date_gmt":"2020-09-23T05:41:53","guid":{"rendered":"http:\/\/4563.org\/?p=161841"},"modified":"2020-09-23T13:41:53","modified_gmt":"2020-09-23T05:41:53","slug":"%e4%ba%9a%e9%a9%ac%e9%80%8a%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%ae%89%e6%8e%92%e8%af%be%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=161841","title":{"rendered":"\u4e9a\u9a6c\u900a\u9762\u8bd5\u9898\uff1a\u5b89\u6392\u8bfe\u7a0b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e9a\u9a6c\u900a\u9762\u8bd5\u9898\uff1a\u5b89\u6392\u8bfe\u7a0b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f60\u9700\u8981\u53bb\u4e0a n \u95e8\u4e5d\u7ae0\u7684\u8bfe\u624d\u80fd\u83b7\u5f97 offer\uff0c\u8fd9\u4e9b\u8bfe\u88ab\u6807\u53f7\u4e3a 0 \u5230 n-1 \u3002<\/p>\n<p>\u6709\u4e00\u4e9b\u8bfe\u7a0b\u9700\u8981\u201c\u524d\u7f6e\u8bfe\u7a0b\u201d\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u8981\u4e0a\u8bfe\u7a0b 0\uff0c\u4f60\u9700\u8981\u5148\u5b66\u8bfe\u7a0b 1\uff0c\u6211\u4eec\u7528\u4e00\u4e2a\u5339\u914d\u6765\u8868\u793a\u4ed6\u4eec\uff1a [0,1] \u7ed9\u4f60\u8bfe\u7a0b\u7684\u603b\u6570\u91cf\u548c\u4e00\u4e9b\u524d\u7f6e\u8bfe\u7a0b\u7684\u9700\u6c42\uff0c\u8fd4\u56de\u4f60\u4e3a\u4e86\u5b66\u5b8c\u6240\u6709\u8bfe\u7a0b\u6240\u5b89\u6392\u7684\u5b66\u4e60\u987a\u5e8f\u3002<\/p>\n<p>\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u6b63\u786e\u7684\u987a\u5e8f\uff0c\u4f60\u53ea\u8981\u8fd4\u56de\u4e00\u79cd\u5c31\u53ef\u4ee5\u4e86\u3002\u5982\u679c\u4e0d\u53ef\u80fd\u5b8c\u6210\u6240\u6709\u8bfe\u7a0b\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<h2>\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165: n = 2, prerequisites = [[1,0]]  \u8f93\u51fa: [0,1] <\/code><\/pre>\n<h2>\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165: n = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]  \u8f93\u51fa: [0,1,2,3] or [0,2,1,3] <\/code><\/pre>\n<h2>\u89e3\u9898\u601d\u8def<\/h2>\n<p>\u5bf9\u4e8e\u4e24\u95e8\u8bfe\u4e4b\u95f4\u7684\u7ea6\u675f\u5173\u7cfb\uff0c\u5f88\u5bb9\u6613\u8054\u60f3\u5230\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8bfe\u62bd\u8c61\u4e3a\u8282\u70b9\uff0c\u5c06\u7ea6\u675f\u62bd\u8c61\u4e3a\u4e00\u6761\u6709\u5411\u8fb9\uff0c\u53ef\u4ee5\u7528\u6709\u5411\u56fe\u7684\u76f8\u5173\u7b97\u6cd5\u89e3\u51b3\u95ee\u9898\u3002\u62d3\u6251\u6392\u5e8f\u6b63\u597d\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<h2>\u7b97\u6cd5\uff1a\u62d3\u6251\u6392\u5e8f<\/h2>\n<p>\u4e00\u4e2a\u5408\u6cd5\u7684\u9009\u8bfe\u5e8f\u5217\u5c31\u662f\u4e00\u4e2a\u62d3\u6251\u5e8f\uff0c\u62d3\u6251\u5e8f\u662f\u6307\u4e00\u4e2a\u6ee1\u8db3\u6709\u5411\u56fe\u4e0a\uff0c\u4e0d\u5b58\u5728\u4e00\u6761\u8fb9\u51fa\u8282\u70b9\u5728\u5165\u8282\u70b9\u540e\u7684\u7ebf\u6027\u5e8f\u5217\uff0c\u5982\u679c\u6709\u5411\u56fe\u4e2d\u6709\u73af\uff0c\u5c31\u4e0d\u5b58\u5728\u62d3\u6251\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u62d3\u6251\u6392\u5e8f\u7b97\u6cd5\u6765\u5f97\u5230\u62d3\u6251\u5e8f\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u5b58\u5728\u73af\u3002<\/p>\n<h3>\u62d3\u6251\u6392\u5e8f\u6b65\u9aa4\uff1a<\/h3>\n<ol>\n<li>\u5efa\u56fe\u5e76\u8bb0\u5f55\u6240\u6709\u8282\u70b9\u7684\u5165\u5ea6\u3002<\/li>\n<li>\u5c06\u6240\u6709\u5165\u5ea6\u4e3a 0 \u7684\u8282\u70b9\u52a0\u5165\u961f\u5217\u3002<\/li>\n<li>\u53d6\u51fa\u961f\u9996\u7684\u5143\u7d20 now\uff0c\u5c06\u5176\u52a0\u5165\u62d3\u6251\u5e8f\u5217\u3002<\/li>\n<li>\u8bbf\u95ee\u6240\u6709 now \u7684\u90bb\u63a5\u70b9 nxt\uff0c\u5c06 nxt \u7684\u5165\u5ea6\u51cf 1\uff0c\u5f53\u51cf\u5230 0 \u540e\uff0c\u5c06 nxt \u52a0\u5165\u961f\u5217\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 3 \u30014\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\u3002<\/li>\n<li>\u5982\u679c\u62d3\u6251\u5e8f\u5217\u4e2a\u6570\u7b49\u4e8e\u8282\u70b9\u6570\uff0c\u4ee3\u8868\u8be5\u6709\u5411\u56fe\u65e0\u73af\uff0c\u4e14\u5b58\u5728\u62d3\u6251\u5e8f\u3002<\/li>\n<\/ol>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<p>\u8bbe\u8bfe\u7a0b\u6570\uff0c\u5373\u56fe\u7684\u8282\u70b9\u6570\u4e3a V \u3002 \u7ea6\u675f\u6570\u91cf\uff0c\u5373\u56fe\u7684\u8fb9\u6570\u4e3a E \u3002 \u65f6\u95f4\u590d\u6742\u5ea6 O(V + E)<\/p>\n<ul>\n<li>\u5efa\u56fe\uff0c\u626b\u63cf\u4e00\u904d\u6240\u6709\u7684\u8fb9 O(E)\u3002<\/li>\n<li>\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5165\u961f\u51fa\u961f 1 \u6b21\uff0c\u590d\u6742\u5ea6 O(V)\u3002<\/li>\n<li>\u90bb\u63a5\u8868\u6700\u7ec8\u4f1a\u88ab\u904d\u5386 1 \u904d\uff0c\u590d\u6742\u5ea6 O(E)\u3002<\/li>\n<li>\u7efc\u4e0a\uff0c\u603b\u590d\u6742\u5ea6\u4e3a O(V + E)\u3002 \u7a7a\u95f4\u590d\u6742\u5ea6 O(V + E)<\/li>\n<li>\u90bb\u63a5\u8868\u5360\u7528 O(V + E)\u7684\u7a7a\u95f4\u3002<\/li>\n<li>\u961f\u5217\u6700\u52a3\u60c5\u51b5\u5199\u5360\u7528 O(V)\u7684\u7a7a\u95f4\u3002<\/li>\n<li>\u7efc\u4e0a\uff0c\u603b\u590d\u6742\u5ea6\u4e3a O(V + E)\u3002<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/*      * @param numCourses: a total of n courses      * @param prerequisites: a list of prerequisite pairs      * @return: the course order      *\/     public int[] findOrder(int numCourses, int[][] prerequisites) {         List[] graph = new ArrayList[numCourses];         int[] inDegree = new int[numCourses];                  for (int i = 0; i &lt; numCourses; i++) {             graph[i] = new ArrayList&lt;Integer&gt;();         }                  \/\/ \u5efa\u56fe         for (int[] edge: prerequisites) {             graph[edge[1]].add(edge[0]);             inDegree[edge[0]]++;         }                  int numChoose = 0;         Queue queue = new LinkedList();         int[] topoOrder = new int[numCourses];                  \/\/ \u5c06\u5165\u5ea6\u4e3a 0 \u7684\u7f16\u53f7\u52a0\u5165\u961f\u5217         for(int i = 0; i &lt; inDegree.length; i++){             if (inDegree[i] == 0) {                 queue.add(i);             }         }                  while (! queue.isEmpty()) {             int nowPos = (int)queue.poll();             topoOrder[numChoose] = nowPos;             numChoose++;             \/\/ \u5c06\u6bcf\u6761\u8fb9\u5220\u53bb\uff0c\u5982\u679c\u5165\u5ea6\u964d\u4e3a 0\uff0c\u518d\u52a0\u5165\u961f\u5217             for (int i = 0; i &lt; graph[nowPos].size(); i++) {                 int nextPos = (int)graph[nowPos].get(i);                 inDegree[nextPos]--;                 if (inDegree[nextPos] == 0) {                     queue.add(nextPos);                 }             }         }                  if (numChoose == numCourses)             return topoOrder;         return new int[0];     } }  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3509471\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : est <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e9a\u9a6c\u900a\u9762\u8bd5\u9898\uff1a\u5b89\u6392\u8bfe\u7a0b \u8cc7\u6df1\u5927\u4f6c &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\/161841"}],"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=161841"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161841\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161841"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}