public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
Queue<Integer> queue = new LinkedList<Integer>();
for(int[] pair:prerequisites){
indegree[pair[1]]++;
}
for(int i=0;i<indegree.length;i++){
if(indegree[i]==0){
queue.add(i);
}
}
int select = 0;
while(!queue.isEmpty()){
numCourses--;
int course = queue.poll();
for(int[] pair:prerequisites){
if(pair[0]==course){
indegree[pair[1]]--;
if(indegree[pair[1]]==0){
queue.add(pair[1]);
}
}
}
}
return numCourses==0;
}
}
207. Course Schedule
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- There are a total of n courses you have to take, labeled ...
- Medium mock interview的时候美国小哥提到了Kahn's Algorithm, 问我会不会。我没...
- There are a total of n courses you have to take, labeled ...
- There are a total of n courses you have to take, labeled ...
- There are a total of n courses you have to take, labeled ...