Course Schedule II

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求

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, return the ordering of courses you should take to finish all courses.

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

Example 1:
Input: 2, [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished
course 0. So the correct course order is [0,1] .

Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output:[0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both
courses 1 and 2. Both courses 1 and 2 should be 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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

您必须参加总共n门课程,标记为0到n-1。

有些课程可能有先决条件,例如,要修课程0,你必须先修课程1,表示为一对:[0,1]

根据课程总数和先决条件对列表,返回完成所有课程所需的课程顺序。

可能有多个正确的订单,您只需要返回其中一个订单。 如果无法完成所有课程,请返回一个空数组。

注意:
输入先决条件是由边列表而不是邻接矩阵表示的图。 详细了解图表的表示方式。
2.您可以假设输入先决条件中没有重复的边缘。

2,题目思路

对于这道题,题目是给定一系列的二元对,second是first的先决条件,判断一些列的二元对是否构成可行的逻辑关系并返回逻辑序列。

这道题,是之前的一道同样的题的改版:
Course Schedule
只需要在判断是否可行的基础上,记录序列即可。

3,代码实现

1,BFS

static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        Graph G = buildGraph(numCourses, prerequisites);
        vector<int> degree = calIndegree(G);
        vector<int> order;
        
        for(int i = 0;i<numCourses;i++){
            int j = 0;
            while(j<numCourses){
                if(degree[j] == 0){  //找到入度为0的点
                    order.push_back(j);
                    break;
                }
                j++;
            }
            if(j == numCourses) //没找到入度为0的点,说明图中有环
                return {};
            //因为degree[j]此时为0,所以--使得j的入度为-1
            //类似于将节点j从图中删除
            degree[j]--;    
            for(auto adj : G[j])
                degree[adj]--;  //因为我们“将节点j从图中删除”,因此将节点j相邻的节点的入度都减一
        }
        return order;
    }
private:
    typedef vector<vector<int>> Graph;  //定义一个有向图
    
    Graph buildGraph(int numCourses, vector<vector<int>>& prerequisites){
        Graph G(numCourses);
        //构建有向图,其中second指向first
        for(auto p : prerequisites)
            G[p[1]].push_back(p[0]);
        return G;
    }
    
    //计算每个节点的入度
    vector<int> calIndegree(Graph& G){
        vector<int> degree(G.size(), 0);
        for(auto v : G)
            for(auto adj : v)
                degree[adj]++;
        return degree;
    }
};

2,DFS

static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<bool> discovered(numCourses, false), finished(numCourses, false);
        vector<int> ans;
        vector<vector<int>> graph(numCourses);
        for (auto& pair : prerequisites) {
            graph[pair[0]].push_back(pair[1]);
        }
        for (int i = 0; i < numCourses; i++) {
            if (!dfs(graph, i, discovered, finished, ans)) return vector<int>();
        }
        return ans;
    }
    
    bool dfs(vector<vector<int>>& graph, int cur, vector<bool>& discovered, vector<bool>& finished, vector<int>& ans) {
        if (finished[cur]) return true;
        if (discovered[cur]) return false;
        discovered[cur] = true;
        for (int neighbor : graph[cur]) {
            if (!dfs(graph, neighbor, discovered, finished, ans)) return false;
        }
        finished[cur] = true;
        ans.push_back(cur);
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值