`
hcx2013
  • 浏览: 83106 次
社区版块
存档分类
最新评论

Course Schedule

 
阅读更多

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

 

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        ArrayList<Set<Integer>> arrayList = new ArrayList<>();
        for (int i = 0; i < numCourses; i++) {
        	arrayList.add(new HashSet<Integer>());
		}
        for (int i = 0; i < prerequisites.length; i++) {
        	arrayList.get(prerequisites[i][1]).add(prerequisites[i][0]);
		}
        int[] preNum = new int[numCourses];
        for (int i = 0; i < numCourses; i++) {
        	Set<Integer> set = arrayList.get(i);
        	Iterator<Integer> iterator = set.iterator();
        	while (iterator.hasNext()) {
        		preNum[iterator.next()]++;
        	}
		}
        for (int i = 0; i < numCourses; i++) {
			int j = 0;
			for (; j < numCourses; j++) {
				if (preNum[j] == 0) {
					break;
				}
			}
			if (j == numCourses) {
				return false;
			}
			preNum[j] = -1;
			Set<Integer> set = arrayList.get(j);
			Iterator<Integer> iterator = set.iterator();
			while (iterator.hasNext()) {
        		preNum[iterator.next()]--;
        	}
		}
        return true;
    }
}

 

0
2
分享到:
评论

相关推荐

    ECSU Course Schedule Assistant-crx插件

    确定课程列表的可能时间表 这是一项扩展功能,可帮助康涅狄格州立大学的学生选择时间表。 ...该扩展程序然后显示一个新页面,其中显示可用的课程表,确保不存在时间冲突。 然后,学生可以通过选择包括或排除课程的特定...

    CourseSchedule:软件项目挑战

    课程时间表 열어버렸네제인데인데로열어버렸네? 내버린로보보다?...如果在Schedule中插入更多属性或删除某些现有属性,则还需要修改ScheduleList和gui Schedule I / O Form(如ScheduleTable和InputPanel)。

    Course Schedule Generator-开源

    完成的项目。 有些功能并不完全智能,但它们可以工作。 如果它阻止使用程序,请报告错误。 该程序是完全开源的,并使用诺基亚 qt [http://qt.nokia.com/] 制作。 联系以获取更多信息或问题。

    University Course Schedule Builder-开源

    为大学制定最佳的上课时间表。

    Pretty UW Course Schedule-crx插件

    语言:English 美化UWaterloo课程表页面! 一个简单的Chrome扩展程序,改善了滑铁卢大学课程表页面的外观。 1.0.7中的新增功能:*扩展名将在新的课程表URL(classes.uwaterloo.ca)上激活。功能包括:*重组的课程搜索...

    ScheduleViz: Course Schedule Plannner-开源

    使用时间表数据的可视化交互式大学课程安排计划者。 通过perl CGI脚本符合标准的模板化Web界面。

    PeopleSoft Course Schedule Export-crx插件

    语言:English 一键导出课程表,并在PeopleSoft网站上提供有用的快捷方式 功能此扩展可帮助学生有效地跟踪他们的课程,最大程度地减少混乱和错过的课程。 它与学校的课程选择网站集成,自动将课程信息解析为重复的...

    漂亮的UW课程表「Pretty UW Course Schedule」-crx插件

    改进观看滑铁卢大学课程表的页面 一个简单的Chrome扩展程序,改善了滑铁卢大学课程表页面的外观。 1.0.4中的新增功能:-在某些情况下修复了类具有“封闭部分”的表格渲染-改进了表单上最新术语的计算功能包括:*重组...

    uiuc:用于I of Course ScheduleCatalog API的JS包装器

    联合会 用于I of Course Schedule / Catalog API的JS包装器安装$ npm install --save uiuc用法该项目使用了一些更流行的ES6功能,例如类,承诺和箭头功能。 导入或要求import uiuc from 'uiuc' // ES6 Syntaxvar ...

    web-schedule-course-master.zip

    web-schedule-course-master.zip

    UBC-RMP-crx插件

    course&dept=econ&course=102 - https://courses.students.ubc.ca/cs/courseschedule?pname=subjarea&tname=subj-section&dept=apsc&course=100&esection=101## 为什么注册课程时UBC学生的典型工作流程是: 1.在...

    多线程leetcode-LeetCode:某物

    _210_CourseSchedule2_BFS_DFS_TopoLogicalSort 两个指针 _75_SortColors_twopointer _142_LinkedListCycle2_twopointer //////// 弗洛伊德循环检测 _287_FindtheDuplicateNumber_TwoPointer_Floyd _340_...

    course-scheduler:用于课程表生成的线性编程模型

    (其中包括其他实用程序,例如jam_in_course.py ,其开发目的是帮助确定如何将一个班级分成两个部分,或者在哪里添加新课程而不必重新计算整个时间表。 对于高级用户,可以编辑solve_schedule.py第182-183行,以便...

    Tufts-Schedule-Generator:塔夫茨大学课程的自动学期时间表生成器

    塔夫茨大学课程的学期时间表生成器免责声明:个人项目。 不被塔夫茨大学认可或隶属塔夫茨大学。...course = COURSEID 例如 获取多门课程的信息终点:/ courses / 查询:?courses = COURSEID&courses = COURSEID 例如

    better_deep_learning_mini_course.pdf

    本文档讲述了如何构建更好的深度学习网络框架,batch size对预测误差和学习率的影响,如何动态修改学习率(Learning Rate Schedule),Batch Normalization对神经网络训练的加速,Weight Regularization对过拟合的...

    USC课程观察员「USC course watcher」-crx插件

    它会将您重定向到USC Schedule of Schedule网站,您可以在其中轻松识别课程部分的编号。警告:显然,官方的USC时间表类网站不会像USC注册系统那样立即更新。由于此扩展程序的所有数据都是从USC班级计划网站上抓取的...

    Electric motor and drives

    But my tight schedule has dela yed the tra nslation. However, four year s ago, several fore ign student s and visitors attende d my gradu ate course class and they need some stud y materia ls so I ...

    psp学习资料

    General • Fixed numerous typographical and inconsistencies defects. &lt;br&gt;Collateral • Added collateral materials (course checklist, facilities specification, and pre-course letters) for ...

Global site tag (gtag.js) - Google Analytics