Leetcode-course Schedule

Source: Internet
Author: User

Title: Leetcode

Course Schedule

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, are it possible for your to finish all courses?

For example:

2, [[1,0]]

There is a total of 2 courses to take. To take course 1 should has finished course 0. So it is possible.

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

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


Analysis:

This topic can be translated into "Judging whether there is a ring in the graph".

1. Put the contents of the array prerequisites into the hash table Table,table key is the course number, value is an array that records the constraints of the course that key represents (that is, the course in value is completed before the key course). If each course is a point on a plane, then each course in key and value can be connected to a single path.

2, using backtracking method, if there is no ring, then the corresponding "path" delete, until the map to delete the light. If there is a loop, the entire program returns FALSE.


Class Solution {Public:bool canfinish (int numcourses, vector<vector<int>>& prerequisites) {if (                Prerequisites.size () <=1) return true;        unordered_map<int,vector<int>> table;        for (auto &i:prerequisites) {table[i[0]].push_back (i[1]);        } vector<int> path;            while (!table.empty ()) {Auto It=table.begin ();            Path.push_back (It->first);            if (Hasloop (Table,it->first,path)) return false;        Path.pop_back ();    } return true; }//To Determine if there are rings, there are rings <span style= "font-family:arial, Helvetica, Sans-serif;" > The words return True, otherwise return false </span> bool Hasloop (unordered_map<int,vector<int>> &table,const int &       Begin,vector<int> &path) {if (Table.count (begin) ==0) return false;           while (!table[begin].empty ()) {int temp=table[begin].back (); if (Find (Path.begin (), path.end (), temp)!=path.end ()) return true;           Path.push_back (temp);            if (Hasloop (Table,temp,path)) return true;            Path.pop_back ();       Table[begin].pop_back ();       } table.erase (begin);    return false; }    };


Leetcode-course Schedule

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.