Course Schedule -- leetcode

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

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?

For example:

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.

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.

基本思路:拓扑排序

课程之间有依赖。

1.找出那些前置条件已经具备的课程进行学习。

2.学完该课程后,更新依赖该门课程的其他课程。

3. 重复步骤1和2.

算法中,维持一个度数数组,或者称前置课程数组degree。表示要学习该课程时,尚需要学习的前置课程数。

如果其值为0,则该课程可以开始学习了。

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<int> degree(numCourses);
        unordered_set<int> finished_courses;
        for (auto p: prerequisites)
            ++degree[p.first];

        for (int i=0; i<numCourses; i++) {
            if (!degree[i])
                finished_courses.insert(i);
        }

        int count = 0;
        while (!finished_courses.empty()) {
            int course = *finished_courses.begin();
            finished_courses.erase(finished_courses.begin());
            for (auto p: prerequisites) {
                if (p.second == course && !--degree[p.first])
                    finished_courses.insert(p.first);
            }
            ++count;
        }

        return count == numCourses;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-30 02:59:08

Course Schedule -- leetcode的相关文章

Course Schedule ——LeetCode

There are a total of n courses you have to take, labeled from 0 to n - 1. 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 l

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

LeetCode Course Schedule II

原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 是Course Schedule的进阶版题目.采用的是同一种方法,这里只需要返回一个结果,所以遍历过的节点放入array中即可. AC Java: 1 public class Solution { 2 public int[] findOrder(int numCourses, int[][] prerequisites) { 3 int [] res = new int[num

LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到,在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环. 代码: class Solution { public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { // [0, {1, 2, 3}], me

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. 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 an

[LeetCode] Course Schedule II 课程清单之二

There are a total of n courses you have to take, labeled from 0 to n - 1. 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 l

leetcode 207. Course Schedule

https://leetcode.com/problems/course-schedule/#/description 图的算法题 题目就是求一堆课程有先修和后修的区别,这些课程能否构成拓扑排序而不矛盾: 应该算是我做的第一个关于图的算法题,一开始毫无思路,看了网上的sloutions,发现这道题还是比较简单的,可以用bfs和dfs都可以做.感觉还是比较典型的关于图算法例题. http://www.cnblogs.com/zmyvszk/p/5573234.html 这个题解讲的还是比较明白的,

【LeetCode】210. Course Schedule II

Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. 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

【LeetCode】207. Course Schedule

Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. 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