当前位置: 首页 > news >正文

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




输入,需要选择的课程数量n,和课程依赖关系pres,输出选课的数组。对于课程a依赖课程b的依赖关系的表示为[a,b]。


思路:
创建依赖关系列表,存放所有依赖其他课才能选的课,数量表示依赖课程的数量。即初始化dependency[n] ,遍历pres[0...n] 将 dependency[pres[i,1]] += 1;
每次找出课程集合:{不依赖任何课程的课}。 将其添加到选课结果中并更新依赖关系列表dependency[n],直到{不依赖任何课程的课}为空集为止。 是一个BFS的过程。
查找{不依赖任何课程的课}: 遍历依赖关系表: pres[i] ,其中i∈[0,n-1] ,判断dependency[pres[i,0]] 是否大于0即可。








实现代码:




public class Solution {
    
public int[] FindOrder(int numCourses, int[,] prerequisites) 
{
	var row = prerequisites.GetLength(0);
	if(row == 0){
        int[] c = new int[numCourses];
        for(int i=0; i<numCourses; i++){
            c[i]=i;
        }
        return c;
    }
	
	// save [courseId, refCount]
	var refCountTable = new int[numCourses];
	
	for(var i = 0;i < row; i++){
		refCountTable[prerequisites[i,0]] ++;
	}
	
	// no ref courses
	var noRefCourses = new Dictionary<int, int>();
	for(var i = 0;i < row; i++){
		// find courses does not appear in ref table
		if(refCountTable[prerequisites[i,1]] == 0 && !noRefCourses.ContainsKey(prerequisites[i,1])){
			noRefCourses.Add(prerequisites[i,1],1);
		}
	}
	
	var size = noRefCourses.Count;
	
	var result = new int[numCourses];
	var count = 0;
	while(noRefCourses.Count > 0){
		var c = noRefCourses.Keys.First();
		result[count++] = c;
		noRefCourses.Remove(c);
		
		for(var i = 0;i < row; i++){
			if(prerequisites[i,1] == c){
				refCountTable[prerequisites[i,0]] --;
				
				if(refCountTable[prerequisites[i,0]] == 0 && !noRefCourses.ContainsKey(prerequisites[i,1])){
					noRefCourses.Add(prerequisites[i,0], 1);
					size++;
				}
			}
		}
	}
	
	if(size == numCourses){
		return result;
	}
	return new int[0];
	
}




}


相关文章:

  • [Real world Haskell] 中文翻译:第二章 类型与函数
  • LeetCode -- Next Permutation
  • [Real world Haskell] 中文翻译:第三章 定义类型,流式函数
  • LeetCode -- Search Matrix
  • LeetCode -- Sort Colors
  • 理解HTTP消息头
  • LeetCode -- Convert SortedList To BST
  • HTTP协议返回状态码表
  • LeetCode -- Insert Interval
  • 推荐WPF的好书
  • LeetCode -- Longest Valid Parentheses
  • 利用Intel博锐技术解决桌面管理难题
  • LeetCode -- Permutations
  • LeetCode -- Construct Binary Tree from Inorder and Postorder Traversal
  • 王小云:十年破译五部顶级密码
  • 【译】JS基础算法脚本:字符串结尾
  • 03Go 类型总结
  • 78. Subsets
  • Bytom交易说明(账户管理模式)
  • const let
  • golang 发送GET和POST示例
  • IDEA 插件开发入门教程
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • JAVA并发编程--1.基础概念
  • mysql 5.6 原生Online DDL解析
  • python_bomb----数据类型总结
  • web标准化(下)
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 聊聊hikari连接池的leakDetectionThreshold
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 中文输入法与React文本输入框的问题与解决方案
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • UI设计初学者应该如何入门?
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • # Java NIO(一)FileChannel
  • (3)(3.5) 遥测无线电区域条例
  • (补)B+树一些思想
  • (六)Hibernate的二级缓存
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (译)2019年前端性能优化清单 — 下篇
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • .htaccess配置重写url引擎
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Core 成都线下面基会拉开序幕
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • []AT 指令 收发短信和GPRS上网 SIM508/548
  • [20160902]rm -rf的惨案.txt
  • [20170705]diff比较执行结果的内容.txt