- 浏览: 185123 次
- 性别:
- 来自: 济南
文章分类
最新评论
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].
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
这道题目与Course Schedule相比要输出一个可能的结果集,如果存在环就输出一个空集。我们同样采用DFS和BFS两种方法来解决。问题在于何时记录结果,用DFS时,我们在递归完成时将当前元素加入结果集;对于BFS,每次从队列中取元素后,将元素加入结果集。代码如下:
DFS:
BFS:
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].
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
这道题目与Course Schedule相比要输出一个可能的结果集,如果存在环就输出一个空集。我们同样采用DFS和BFS两种方法来解决。问题在于何时记录结果,用DFS时,我们在递归完成时将当前元素加入结果集;对于BFS,每次从队列中取元素后,将元素加入结果集。代码如下:
DFS:
public class Solution { HashSet<Integer> isVisited; boolean[] onStack; LinkedList<Integer> list; public int[] findOrder(int numCourses, int[][] prerequisites) { int[] result = new int[numCourses]; if(prerequisites == null || prerequisites.length == 0) { for(int i = 0; i < numCourses; i++) { result[i] = i; } return result; } list = new LinkedList<Integer>(); onStack = new boolean[numCourses]; isVisited = new HashSet<Integer>(); List<Integer>[] graph = new List[numCourses]; for(int i = 0; i < prerequisites.length; i++) { int key = prerequisites[i][1]; int value = prerequisites[i][0]; if(graph[key] == null) graph[key] = new ArrayList<Integer>(); graph[key].add(value); } for(int i = 0; i < numCourses; i++) { if(!isVisited.contains(i) && hasCycle(i, graph) == true) return new int[0]; } for(int i = 0; i < list.size(); i++) { result[i] = list.get(i); } return result; } public boolean hasCycle(int i, List<Integer>[] graph) { isVisited.add(i); onStack[i] = true; boolean cycle = false; if(graph[i] != null) { for(int c : graph[i]) { if(!isVisited.contains(c)) { cycle = hasCycle(c, graph); if(cycle == true) break; } else if(onStack[c] == true) { cycle = true; break; } } } list.addFirst(i); onStack[i] = false; return cycle; } }
BFS:
public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] result = new int[numCourses]; if(prerequisites == null || prerequisites.length == 0) { for(int i = 0; i < numCourses; i++) result[i] = i; return result; } int edges = prerequisites.length; int start = 0; int[] getIndex = new int[numCourses]; Queue<Integer> qSet = new LinkedList<Integer>(); List<Integer>[] graph = new List[numCourses]; for(int i = 0; i < prerequisites.length; i++) { getIndex[prerequisites[i][0]] ++; int key = prerequisites[i][1]; int value = prerequisites[i][0]; if(graph[key] == null) graph[key] = new ArrayList<Integer>(); graph[key].add(value); } for(int i = 0; i < getIndex.length; i++) { if(getIndex[i] == 0) qSet.offer(i); } while(!qSet.isEmpty()) { int tem = qSet.poll(); result[start++] = tem; if(graph[tem] != null) { for(int c : graph[tem]) { edges --; if(--getIndex[c] == 0) qSet.offer(c); } } } if(edges != 0) return new int[0]; return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 272You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 390Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 504Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 672Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 474The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 435Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 592Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 430All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 907Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 607Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 695Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 862Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 791You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 729For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Course Schedule III.java
11. Course Schedule II问题 可以使用BFS的方法,逐渐抹去入度为0的节点以及他们的边,将大问题化小。 12. 买卖股票最佳时期问题 可以使用hard级别的方法,两次,求最大收益。 13. 岛屿最大面积系列问题 可以...
python python_leetcode题解之207_Course_Schedule.py
"University Course Schedule Builder",一款开源的课程日程规划工具,以其独特的优势,为大学提供了定制化、高效且灵活的课表解决方案。 开源软件的一大优势在于其开放源代码,允许用户自由查看、修改和分发代码。...
"web-schedule-course-master.zip"这个压缩包文件,很可能是某个在线课程或教程的资源集合,专注于讲解Web调度相关的知识。通过分析其文件结构,我们可以深入探讨Web调度的核心概念、应用场景以及实现技术。 Web...
确定课程列表的可能时间表 这是一项扩展功能,可帮助康涅狄格州立大学的学生选择时间表。 ...该扩展程序然后显示一个新页面,其中显示可用的课程表,确保不存在时间冲突。 然后,学生可以通过选择包括或排除课程的特定...
课程时间表 열어버렸네제인데인데로열어버렸네? 내버린로보보다?...如果在Schedule中插入更多属性或删除某些现有属性,则还需要修改ScheduleList和gui Schedule I / O Form(如ScheduleTable和InputPanel)。
例如,我们可以使用`List<Course>`来存储多个课程对象,其中`Course`是自定义的课程类。 3. 枚举(Enum):我们可以用枚举来定义课程的类型(如数学、英语、体育等),这样可以提供更好的代码可读性和类型安全性。 ...
"course-table源码"是一个专为Android平台设计的项目,主要功能是实现课程表的展示与管理。这个源码提供了一个很好的示例,对于想要学习Android应用开发,特别是对UI设计和数据管理感兴趣的开发者来说,这是一个值得...
完成的项目。 有些功能并不完全智能,但它们可以工作。 如果它阻止使用程序,请报告错误。 该程序是完全开源的,并使用诺基亚 qt [http://qt.nokia.com/] 制作。 联系以获取更多信息或问题。
3. 图论:如“Course Schedule II”可能涉及拓扑排序和深度优先搜索。 4. 字符串处理:如“Valid Palindrome II”要求实现字符串的双指针法或者动态规划解决。 5. 排序与搜索:比如“Search in Rotated Sorted Array...
Course Schedule II**:这个问题可以通过模拟LRU缓存来解决,其中每个课程代表一个元素,课程的依赖关系决定了元素的使用顺序。 2. **146. LRU Cache**:这是一个直接要求实现LRU缓存的题目,是理解LRU机制的好例子...
语言:English 一键导出课程表,并在PeopleSoft网站上提供有用的快捷方式 功能此扩展可帮助学生有效地跟踪他们的课程,最大程度地减少混乱和错过的课程。 它与学校的课程选择网站集成,自动将课程信息解析为重复的...
语言:English 美化UWaterloo课程表页面! 一个简单的Chrome扩展程序,改善了滑铁卢大学课程表页面的外观。 1.0.7中的新增功能:*扩展名将在新的课程表URL(classes.uwaterloo.ca)上激活。功能包括:*重组的课程搜索...
**ScheduleViz:开源课程日程规划器** ScheduleViz是一个专为大学学生和教育机构设计的开源项目,它提供了一个交互式的、基于时间表数据的课程安排工具。这款工具利用Perl CGI(Common Gateway Interface)脚本来...
Course Schedule II`(课程表II)、`202. Happy Number`(快乐数)、`146. LRU Cache`(LRU 缓存机制)等。 4. **LRUCache 应用场景**: - 在数据库查询优化中,LRUCache 用于缓存最近查询过的数据,减少对数据库...
Course Schedule II" 和 "146. LRU Cache"。这些问题要求我们设计并实现一个高效的缓存机制,LRU Cache 是理想的解决方案。通过分析和解答这些题目,我们可以加深对 LRUCache 工作原理的理解,并提升 Java 编程技巧...
Course Schedule II`、`212. Word Search II`以及著名的`146. LRU Cache`。这些问题可以帮助理解LRU的工作原理,并锻炼实现LRU缓存的数据结构和算法能力。 6. **应用**: - LRU缓存广泛应用于数据库系统、操作...
改进观看滑铁卢大学课程表的页面 一个简单的Chrome扩展程序,改善了滑铁卢大学课程表页面的外观。 1.0.4中的新增功能:-在某些情况下修复了类具有“封闭部分”的表格渲染-改进了表单上最新术语的计算功能包括:*重组...