【拓扑】207. Course Schedule && 210. Course Schedule II

207. Course Schedule【拓扑】

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:

Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: 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.

Constraints:

1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
All the pairs prerequisites[i] are unique.

class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        
        int n=prerequisites.length;
        if(n==0)return true;
       
        int[] inDeg=new int[numCourses];
        PriorityQueue<Integer> myQue=new PriorityQueue<>((n1,n2)->inDeg[n1]-inDeg[n2]);
        ArrayList<Integer>[] myList=new ArrayList[numCourses];

        for(int i=0;i<numCourses;i++){
            myList[i]=new ArrayList<>();
        }



        for(int i=0;i<n;i++){
            int a=prerequisites[i][0];
            int b=prerequisites[i][1];
            inDeg[a]++;
            //inDeg[b]++;
            myList[b].add(a);
        }

        for(int i=0;i<numCourses;i++){
            myQue.add(i);
        }

        while(myQue.size()>0){
            int x=myQue.poll();
            //System.out.println(x);
            if(inDeg[x]!=0)return false;
            for(int y:myList[x]){
                myQue.remove(y);
                inDeg[y]--;
                myQue.add(y);
            }
        }

        return true;
    }
}

写复杂了,没必要用优先队列

BFS

class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        
        int n=prerequisites.length;
        if(n==0)return true;
       
        int[] inDeg=new int[numCourses];
        ArrayList<Integer>[] myList=new ArrayList[numCourses];
        LinkedList<Integer> myQue=new LinkedList<>();

        for(int i=0;i<numCourses;i++){
            myList[i]=new ArrayList<>();
        }


        for(int i=0;i<n;i++){
            int a=prerequisites[i][0];
            int b=prerequisites[i][1];
            inDeg[a]++;
            //inDeg[b]++;
            myList[b].add(a);
        }

        for(int i=0;i<numCourses;i++){
            if(inDeg[i]==0)myQue.add(i);
        }

        int num=0;
        while(myQue.size()>0){
            int x=myQue.remove();
            num++;
            for(int y:myList[x]){
                inDeg[y]--;
                if(inDeg[y]==0)myQue.add(y);
            }
        }

        return num==numCourses;
    }
}

210. Course Schedule II

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: numCourses = 2, prerequisites = [[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: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [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].
Example 3:

Input: numCourses = 1, prerequisites = []
Output: [0]

Constraints:

1 <= numCourses <= 2000
0 <= prerequisites.length <= numCourses * (numCourses - 1)
prerequisites[i].length == 2
0 <= ai, bi < numCourses
ai != bi
All the pairs [ai, bi] are distinct.

class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List<List<Integer>> edge=new ArrayList<>();
        LinkedList<Integer> myQue=new LinkedList<>();
        int[] inDeg=new int[numCourses];

        for(int i=0;i<numCourses;i++){
            edge.add(new ArrayList<>());
        }
        for(int[] p:prerequisites){
            int a=p[0];
            int b=p[1];
            edge.get(b).add(a);
            inDeg[a]++;
        }


        for(int i=0;i<numCourses;i++){
            if(inDeg[i]==0)myQue.add(i);
        }
        ArrayList<Integer> ans=new ArrayList<>();
        while(myQue.size()>0){
            //System.out.println(myQue.size());
            int x=myQue.poll();
            ans.add(x);
            for(int y:edge.get(x)){
                inDeg[y]--;
                if(inDeg[y]==0)myQue.add(y);
            }
        }

        if(ans.size()==numCourses)return ans.stream().mapToInt(Integer::valueOf).toArray();
        else return new int[0];

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值