[Leetcode] 210. Course Schedule II Java

Source: Internet
Author: User

Topic:

There is a total of n courses you have 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 take to fini SH 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] .

test instructions and analysis: given a course sequence, some courses require a pre-order course, and a class order that satisfies the criteria. You can use the topology sort to delete a point with an entry level of 0 each time, save the point with a list for each deletion (the point with a degree of 0), because I put the pre-order course in front of it, so I finally need to reverse the list and save it to the array output. The general and 200 questions are similar.

Code:

 Public classSolution { Public int[] FindOrder (intNumcourses,int[] Prerequisites) {        int[] map=New int[numcourses];  for(inti=0;i<prerequisites.length;i++) {//calculate the penetration of each pointmap[prerequisites[i][0]]++; } Queue<Integer> queue =NewLinkedlist<> ();//A point with a record entry degree of 0         for(inti=0;i<map.length;i++){            if(map[i]==0) Queue.add (i); } List<Integer> res =NewArraylist<>(); intCount = Queue.size ();//The initial point of entry is O         while(!Queue.isempty ()) {            inttemp =Queue.poll ();            Res.add (temp);  for(inti=0;i<prerequisites.length;i++){                if(temp== prerequisites[i][1]){                    intt = prerequisites[i][0]; Map[t]--; if(map[t]==0) {Queue.add (t); Count++; }                }            }        }        if(count!=numcourses) {            int[] A =New int[0]; returnA; }Else {            int[] A =New int[Res.size ()];  for(intI=res.size () -1;i>=0;i--) {//we need to reverse this .A[i] =Res.get (i); }            returnA; }    }}

[Leetcode] 210. Course Schedule II Java

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.