Leetcode-course Schedule II

Source: Internet
Author: User

Title: Leetcode

Course Schedule II 

there is a total Of n  courses Has to take, labeled From 0  to n-1 .

Some courses May has 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, return the ordering of courses you should tak E to finish all courses.

There may is multiple correct orders, you just need to return one of the them. If It is impossible-to-finish all courses, return an empty array.

For example:

2, [[1,0]]

There is a total of 2 courses to take. To take course 1 should has finished course 0. So the correct course order is[0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

there is a total of 4 courses to take. To take course 3 should has finished both courses 1 and 2. Both Courses 1 and 2 should is taken after you finished course 0. So one correct course order Is [0,1,2,3] . Another correct ordering Is[0,2,1,3] .

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

Click to show more hints.

Hints:
    1. This problem was equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it'll be impossible to take all courses.
    2. Topological Sort via dfs-a great video tutorial (minutes) on Coursera explaining the basic concepts of topological so Rt.
    3. Topological sort could also is done via BFS.

Analysis:

Using the topology sort, refer to here.

A simple topological sorting algorithm:

A) First find a point with no input edge, output this point, and then remove all the edges connected to this point.

b) Repeat the above steps to know the output of all points.



Class solution {public:vector<int> FindOrder (int numcourses, vector<pair<int, int>>& Prerequisi        TES) {//input actually has duplicate value!!!        Sort (Prerequisites.begin (), Prerequisites.end ());                 Prerequisites.erase (Unique (Prerequisites.begin (), Prerequisites.end ()), Prerequisites.end ());         vector<int> Res;         if (numcourses==0) return res;            if (Prerequisites.empty ()) {for (int i=0;i<numcourses;++i) res.push_back (i);        return res;        } unordered_map<int,vector<int>> End_start;        for (auto &i:prerequisites) {end_start[i.first].push_back (I.second);        } unordered_set<int> num;        for (int i=0;i<numcourses;++i) num.emplace (i);            while (!num.empty ()) {bool should_return=true;          for (auto &n:num) {if (End_start.find (n) ==end_start.end ())      {should_return=false;                    Res.push_back (n);                    Cache_erase (End_start,n);                    Num.erase (n);                Break        }} if (Should_return) return vector<int> ();    } return res; } void Cache_erase (unordered_map<int,vector<int>> &end_start,int N) {for (auto It=end_sta        Rt.begin (); It!=end_start.end ();)            {Auto Tmp=find (It->second.begin (), It->second.end (), n);                if (Tmp!=it->second.end ()) {it->second.erase (TMP);                if (It->second.empty ()) {end_start.erase (it++);            } else ++it;        } else ++it; }    }        };


Leetcode-course Schedule II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.