[LeetCode-JAVA] 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.

思路:首先要里解题意 一些课程有先修课程--这些课程必须先学,类似下面的图片,这个就是典型的有向图的拓扑排序

步骤1:找到一个入度为零的顶点

步骤2:从图中删除这个顶点

步骤3:循环1、2 如果所有顶点都走过了 那么就是一个可以完成的图

其次还要了解题中算法的输入 numCourse为要选的课的总数 而prerequisite矩阵是先决条件矩阵(开始一直以为是邻接矩阵)

所以,第一步要先将给出的矩阵,换算出邻接矩阵(也可以是邻接链表),然后再步步进行。

代码:

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] indegree = new int[numCourses];
        int[][] matrix = new int[numCourses][numCourses];  // [i][j] i为j的先决条件 i->j
        Stack<Integer> stack = new Stack<Integer>();

        for(int i = 0 ; i < prerequisites.length ; i++){
            if(matrix[prerequisites[i][1]][prerequisites[i][0]] == 1) continue;//输入有重复
            indegree[prerequisites[i][0]]++; //入度加一
            matrix[prerequisites[i][1]][prerequisites[i][0]] = 1; //p中[j]<-[i]
        }

        for(int i = 0 ; i < numCourses ; i++){
            if(indegree[i] == 0)    //将入度为零的压入栈
                stack.push(i);
        }

        while(!stack.isEmpty()){
            int temp = stack.pop();  //删掉入度为零的点所有连出的线
            for(int i = 0 ; i < numCourses ; i++){  //每删掉一个 将对应的入度减一
                if(matrix[temp][i] == 1){
                    matrix[temp][i] = 0;
                    indegree[i]--;   //如果减去后i对应的入度为0 则将其入栈
                    if(indegree[i] == 0)
                        stack.push(i);
                }
            }
        }

        for(int i = 0 ; i < numCourses ; i++){   //判断是否有入度不为零的点
            if(indegree[i] > 0)
                return false;
        }

        return true;
    }
}
时间: 2024-10-03 20:11:00

[LeetCode-JAVA] Course Schedule的相关文章

【LeetCode】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 an

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode 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? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

Set Matrix Zeroes leetcode java

题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement