207. Course Schedule (易理解拓撲排序解法)

There are a total of n courses you have to take, labeled from 0 to n - 1.express

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]數組

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?ui

For example:spa

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.code

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.blog

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.排序

 

題的本意就是拓撲排序,個人方法比較笨,也容易理解,就是按照拓撲排序的方法解題,464ms。ci

步驟:get

  (1)初始化一個大小爲numCourses的數組grap;input

  (2)將圖中個節點的入度保存到數組中,將數組中第一個入度爲0的節點找出,並刪除與它相關的邊,並將該節點在數組中的值設爲-1,其餘節點賦值爲0;

  (3)重複(2)numCourses次,若圖中邊爲0,這返回true,不然返回false;

  

 1 bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites)
 2 {
 3     int grap[numCourses]={0};
 4     for (int j=numCourses-1;j>=0;--j)
 5     {
 6         int del=-1;
 7         for (int i=0;i<prerequisites.size();i++)
 8             grap[prerequisites[i].first]++;
 9         for (int k=0;k<numCourses;k++)
10         {
11             if (grap[k]==0)
12             {
13                 del=k;
14                 break;
15             }
16         }
17         if(del==-1)
18             break;
19         vector<pair<int, int>>::iterator its;
20         for (its=prerequisites.begin();its!=prerequisites.end();)
21         {
22             if (its->second==del)
23                 its=prerequisites.erase(its);
24             else
25                 ++its;
26         }
27         grap[del]=-1;
28         for (int p=0;p<numCourses;p++)
29         {
30             if (grap[p]!=-1)
31                 grap[p]=0;
32         }
33     }
34     if(prerequisites.size()>0)
35         return false;
36     return true;
37 }

 改進:(340ms)

  維護一個數組,數組中存放圖中各頂點入度,若頂點入度爲0,將數組中全部以該頂點爲入度的頂點入度減一。循環numCourses次,如循環中出現數組中全部頂點度都不爲0,return false.

 

 1 bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites)
 2 {
 3     int grap[numCourses]={0};
 4         
 5     for (int i=0;i<prerequisites.size();i++)
 6         grap[prerequisites[i].first]++;
 7     for (int j=numCourses-1;j>=0;--j)
 8     {
 9         int del=-1;
10         for (int k=0;k<numCourses;k++)
11         {
12             if (grap[k]==0)
13             {
14                 del=k;
15                 grap[k]=-1;
16                 break;
17             }
18         }
19         if(del==-1)
20             return false;
21         for (int i=0;i<prerequisites.size();i++)
22         {
23             if (prerequisites[i].second==del)
24                 grap[prerequisites[i].first]--;
25         }
26     }
27     return true;
28 }
相關文章
相關標籤/搜索