Leetcode-210(Java) 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 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.

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 the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

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].

传送门:https://leetcode.com/problems/course-schedule-ii/

public class Solution {

    //test case [1,0]
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[] map = new int[numCourses];
        int n = prerequisites.length;
        int[] res = new int[numCourses];

        for(int i=0; i<n; i++) {
            map[ prerequisites[i][1] ] ++;
        }

        Queue<Integer> que = new LinkedList<Integer>();
        int index = numCourses-1;
        for(int i=0; i<numCourses; i++) {
            if(map[i] == 0) {
                que.add(i);
                res[index--] = i;
            }
        }

        while(!que.isEmpty() ){
            int k = que.remove();
            for(int i=0; i<n; i++) {
                int l = prerequisites[i][1];
                if(k==prerequisites[i][0]) {
                    map[l]--;
                    if(map[l] == 0) {
                        que.add(l);
                        res[index--] = l;
                    }
                }
            }
        }
        if(index!=-1) return new int[0];
        else return res;
    }
}
时间: 2024-08-27 18:34:39

Leetcode-210(Java) Course Schedule II的相关文章

【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

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

Java for LeetCode 118 Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. 解题思路: 注意,本题的k相当于上题的k+1,其他照搬即可,JAVA实现如下: public List<Integer> getRow(int rowIndex) { List<Integer> alist=new ArrayList<Integer>();

[email&#160;protected] [210]Course Schedule II

Course Schedule II Total Accepted: 13015 Total Submissions: 64067 Difficulty: Medium 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 co

leetcode第一刷_Word Ladder II

注:本文仅供技术探讨, 研究,测试使用. 这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资源并最终崩溃. 最近因为在修补struts1的可操纵classLoader的漏洞(struts2也有该漏洞, 不在本文讨论范围), 所以我就在我建立的struts1的项目上直接做测试,怎么创建struts1的项目不在本文讨论范围之列你可以在这里下载strut

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-triangle-ii/ 题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路: 要求空间复杂度为O

LeetCode 119. Pascal&#39;s Triangle II (杨辉三角之二)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 题目标签:Array 这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那

LeetCode:Pascal‘s Triangle II - 帕斯卡三角形2

1.题目名称 Pascal's Triangle II(帕斯卡三角形2) 2.题目地址 https://leetcode.com/problems/pascals-triangle-ii/ 3.题目内容 英文:Given an index k, return the kth row of the Pascal's triangle. 中文:给出行数k,返回帕斯卡三角形的第k行 例如,k=3时,返回[1,3,3,1] 4.解题方法1 帕斯卡三角形也叫杨辉三角形,在LeetCode第118题(Pas