leetcode 207. 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 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.

这道题考察的就是拓扑排序,关键是图是怎么存储,怎么判断前驱节点,我在网上看到了一个很不错的做法,可以参考一下。

建议和leetcode 210. Course Schedule II 拓扑排序 + HashSetleetcode 630. Course Schedule III 课程调度 + 贪心算法 一起学习

需要注意的是,由于这道题更新了测试用例所以原先的代码可能出现问题,情况是这样的,记录后继结点要使用vector,不要使用set,因为假如存在(1,9)和(1,9)多次出现,这样就会出现问题,所以要使用vector

代码如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;

/*
 * 本质就是判断图中是否有环,拓扑排序的应用
 * http://blog.csdn.net/ljiabin/article/details/45846837
 * */
public class Solution 
{
    public boolean canFinish(int numCourses, int[][] prerequisites) 
    {
        if(numCourses<=1 || prerequisites.length<=1)
            return true;

        int []preCount=new int[numCourses];
        List<Set<Integer>> list=new ArrayList<Set<Integer>>();
        Arrays.fill(preCount, 0);
        for(int i=0;i<numCourses;i++)
            list.add(new HashSet<>());

        /*
         * list保存的是出度边,也即后继结点
         * 这里使用是为了避免重复的边
         * perCount保存的说前驱节点的数量
         * */
        for(int i=0;i<prerequisites.length;i++)
        {
            int to=prerequisites[i][0];
            int from=prerequisites[i][1];
            list.get(from).add(to);
            preCount[to]++;
        }

        //使用队列来拓扑排序
        Queue<Integer> queue=new LinkedList<Integer>();
        for(int i=0;i<numCourses;i++)
        {
            if(preCount[i]==0)
                queue.add(i);
        }

        int res=numCourses;
        while(queue.isEmpty()==false)
        {
            int from=queue.poll();
            res--;

            Set<Integer> one=list.get(from);
            Iterator<Integer> iter =one.iterator();
            while(iter.hasNext())
            {
                int to=iter.next();
                preCount[to]--;
                if(preCount[to]==0)
                    queue.add(to);
            }
        }
        //这里通过res来判断是否所有的课程都可以满足
        return res==0?true:false;
    }
}

下面是C++的做法,这是一道十分典型的拓扑排序的问题,看过答案就会发现本题十分的简单

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>

using namespace std;



class Solution
{
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& reque)
    {
        vector<int> preCount(numCourses, 0);
        vector<vector<int>> next(numCourses, vector<int>());
        for (pair<int, int> key : reque)
        {
            int from = key.second;
            int to = key.first;
            next[from].push_back(to);
            preCount[to]++;
        }

        queue<int> que;
        for (int i = 0; i < preCount.size(); i++)
        {
            if (preCount[i] == 0)
                que.push(i);
        }

        int count = numCourses;
        while (que.empty() == false)
        {
            int from = que.front();
            que.pop();
            count--;
            for (int i: next[from])
            {
                preCount[i]--;
                if (preCount[i] == 0)
                    que.push(i);
            }
        }

        return count == 0 ? true : false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值