Course Schedule Problem
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.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
One simple way to represent a graph is just a list, or array, of |E| edges, which we call an edge list. To represent an edge, we just have an array of two vertex numbers, or an array of objects containing the vertex numbers of the vertices that the edges are incident on. If edges have weights, add either a third element to the array or more information to the object, giving the edge"s weight. Since each edge contains just two or three numbers, the total space for an edge list is Θ(E).
Solution Graph using HashMap+Queuepublic class Solution { public boolean canFinish(int n, int[][] p) { MapGraph using Queue> graph = new HashMap<>(); int[] in = new int[n]; for (int i = 0; i < p.length; i++) { if (!graph.containsKey(p[i][1])) graph.put(p[i][1], new ArrayList<>()); graph.get(p[i][1]).add(p[i][0]); in[p[i][0]]++; } Queue q = new LinkedList<>(); for (int i = 0; i < in.length; i++) { if (in[i] == 0) q.offer(i); } int count = 0; while (!q.isEmpty()) { Integer from = q.poll(); count++; List to = graph.get(from); if (to == null) continue; for (Integer i: to) { in[i]--; if (in[i] == 0) q.offer(i); } } return count == n; } }
public class Solution { public boolean canFinish(int n, int[][] p) { int[] in = new int[n]; for (int i = 0; i < p.length; i++) { in[p[i][0]]++; } QueueCourse Schedule II Problemq = new LinkedList<>(); for (int i = 0; i < in.length; i++) { if (in[i] == 0) q.offer(i); } while (!q.isEmpty()) { int cur = q.poll(); for (int i = 0; i < p.length; i++) { if (cur == p[i][1]) {in[p[i][0]]--; if (in[p[i][0]] == 0) q.offer(p[i][0]);} } } for (int i = 0; i < in.length; i++) { if (in[i] != 0) return false; } return true; } }
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].
public class Solution { public int[] findOrder(int n, int[][] p) { int[] res = new int[n]; int[] in = new int[n]; QueueAlien Dictionary Problemq = new LinkedList<>(); for (int i = 0; i < p.length; i++) { in[p[i][0]]++; } for (int i = 0; i < in.length; i++) { if (in[i] == 0) q.offer(i); } int index = 0; while (!q.isEmpty()) { Integer cur = q.poll(); res[index++] = cur; for (int i = 0; i < p.length; i++) { if (p[i][1] == cur) { in[p[i][0]]--; if (in[p[i][0]] == 0) q.offer(p[i][0]); } } } return index == n ? res: new int[0]; } }
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
For example,
Given the following words in dictionary,
[ "wrt", "wrf", "er", "ett", "rftt" ]
The correct order is: "wertf".
Note:
You may assume all letters are in lowercase.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66100.html
Course Schedule I 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 e...
摘要:拓扑排序复杂度时间空间思路本题和的解法一样,不会拓扑排序的可以参考那篇文章。区别在于我们拓扑排序后的访问顺序,本来我们是用一个来进行,这里为了让依赖少的先安装,我们将换成,并以依赖数排序。 Install Dependencies 给定软件之间安装的依赖关系,用一个二维数组表示,第一维表示依赖的序号,第二维表示依赖关系,比如要先装deps[0][0],才能装deps[0][1]。安装时...
摘要:建立入度组成,把原来输入的无规律,转换成另一种表示图的方法。找到为零的点,放到里,也就是我们图的入口。对于它的也就是指向的。如果这些的入度也变成,也就变成了新的入口,加入到里,重复返回结果。这里题目有可能没有预修课,可以直接上任意课程。 Some courses may have prerequisites, for example to take course 0 you have ...
Problem 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...
Problem 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 a...
阅读 2976·2023-04-26 02:25
阅读 2249·2023-04-25 18:05
阅读 646·2021-09-30 09:57
阅读 2941·2021-09-27 14:10
阅读 1652·2019-08-30 15:44
阅读 1003·2019-08-29 15:28
阅读 2523·2019-08-29 14:10
阅读 2262·2019-08-29 13:30