Course Schedule II Solution

Source: Internet
Author: User

Question

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] .

Solution

Similar with "Course Schedule", and the only difference are that we need to record path.

Note:an Empty array is the array with length = 0.

1  Public classSolution {2      Public int[] FindOrder (intNumcourses,int[] Prerequisites) {3         //This problem was to print one possible topological sort result4         //First , we need to construct a directed graph in the form of adjacency list5list<integer>[] Adjacencylist =Newarraylist[numcourses];6         int[] result =New int[numcourses];7         int[] degree =New int[numcourses];8Arrays.fill (degree, 0);9          for(intj = 0; J < Numcourses; J + +) {TenList<integer> tmplist =NewArraylist<integer>(); One Tmplist.add (j); AADJACENCYLIST[J] =tmplist; -         } -         intLength =prerequisites.length; the          for(intj = 0; J < length; J + +) { -             int[] pair =Prerequisites[j]; -Adjacencylist[pair[1]].add (pair[0]); -degree[pair[0]]++; +         } -          +         //queue is to store nodes with 0 in-degree Aqueue<integer> queue =NewLinkedlist<integer>(); at          for(intj = 0; J < Numcourses; J + +) { -             if(Degree[j] = = 0) - Queue.add (j); -         } -         if(queue.size () = = 0) -             return New int[0]; in         inti = 0; -          to         //begin BFS +          while(Queue.size () > 0) { -             intCurrent =Queue.remove (); theResult[i] =Current ; *List<integer> currentlist =Adjacencylist[current]; $              for(intj = 1; J < Currentlist.size (); J + +) {Panax Notoginseng                 intTMP =Currentlist.get (j); -degree[tmp]--; the                 if(degree[tmp] = = 0) + Queue.add (TMP); A             } thei++; +         } -         if(I <numcourses) $             return New int[0]; $         returnresult; -     } -}

Course Schedule II Solution

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.