Course Schedule I & 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 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.

From: http://www.programcreek.com/2014/05/leetcode-course-schedule-java/

分析:

建立有向图,利用Toplogical sort逐一去掉没有father的节点。如果最后总的节点大于没有父节点的个数,表面里面有环。

 1 public boolean canFinish(int numCourses, int[][] prerequisites) {
 2         if (prerequisites == null) return true;
 3         Map<Integer, Node> map = new HashMap<Integer, Node>();
 4         
 5         for (int[] row : prerequisites) {
 6             if (!map.containsKey(row[0])) {
 7                 map.put(row[0], new Node(row[0], 0));
 8             }
 9             
10             if (!map.containsKey(row[1])) {
11                 map.put(row[1], new Node(row[1], 0));
12             }
13             
14             map.get(row[0]).inDegree++;
15             map.get(row[1]).list.add(map.get(row[0]));
16         }
17         
18         Queue<Node> queue = new LinkedList<Node>();
19         for (Node node : map.values()) {
20             if (node.inDegree == 0) {
21                 queue.offer(node);
22             }
23         }
24         
25         while (!queue.isEmpty()) {
26             Node node = queue.poll();
27             for (Node child : node.list) {
28                 child.inDegree--;
29                 if (child.inDegree == 0) {
30                     queue.offer(child);
31                 }
32             }
33         }
34         for (Node node : map.values()) {
35             if (node.inDegree != 0) {
36                 return false;
37             }
38         }
39         return true;
40         
41     }
42 }
43 
44 class Node {
45     int value;
46     int inDegree;
47     List<Node> list;
48 
49     public Node(int value, int inDegree) {
50         this.value = value;
51         this.inDegree = inDegree;
52         list = new ArrayList<Node>();
53     }
54 }

Course Schedule II

找出选课的顺序。

public class Solution {
    public int[] findOrder(int n, int[][] prerequisites) {
        if (prerequisites == null) return new int[0];
        Map<Integer, Node> map = new HashMap<Integer, Node>();
        // very important
        for (int i = 0; i < n; i++){
            map.put(i, new Node(i, 0));
        }
        
        
        for (int[] row : prerequisites) {
            map.get(row[0]).inDegree++;
            map.get(row[1]).list.add(map.get(row[0]));
        }
        
        List<Integer> list = new ArrayList<Integer>();
        
        Queue<Node> queue = new LinkedList<Node>();
        for (Node node : map.values()) {
            if (node.inDegree == 0) {
                queue.offer(node);
            }
        }
        
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            list.add(node.value);
            for (Node child : node.list) {
                child.inDegree--;
                if (child.inDegree == 0) {
                    queue.offer(child);
                }
            }
        }
        if (list.size() < n) return new int[0];
        int[] result = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
}

class Node {
    int value;
    int inDegree;
    List<Node> list;

    public Node(int value, int inDegree) {
        this.value = value;
        this.inDegree = inDegree;
        list = new ArrayList<Node>();
    }
}

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5948697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值