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?

Example 1:

Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
  To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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:

    1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
    2. You may assume that there are no duplicate edges in the input prerequisites.
  • 解题思路

这道题目是检测图内是否有环的应用题目,解决方案是经典的拓扑排序即可。之前实习面试的时候,太紧张没能想到拓扑排序。

所以,这次也特意选择拓扑排序类的题目做一下。感觉确实是不熟悉,简单的程序也调了挺久。

解这道题的基本思路如下:

    • 针对每个节点,记录依赖它的节点,以及自身依赖节点的数目。
    • 选出依赖节点数目为0的节点,移除,更新依赖它的节点的依赖节点数目。
    • 循环上述步骤,直至无点可移。
    • 判断剩余点数。

结合这个思路,编码实现如下:

class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
//initiate outNodes and inEdges
vector<int> outNodes(numCourses);
vector<vector<int>> inEdges(numCourses);
for(int i = ; i < numCourses; i++)
{
outNodes[i] = ;
}
int n = prerequisites.size();
for(int i = ; i < n; i++)
{
outNodes[prerequisites[i].first] += ;
inEdges[prerequisites[i].second].push_back(prerequisites[i].first);
} int leftNode = numCourses;
bool toFind = true;
while(toFind)
{
// find next node to move
toFind = false;
for(int i = ; i < numCourses; i++)
{
// remove the outNodes and update outNodes vector
if(outNodes[i] == )
{
outNodes[i] -= ;
for(int j = ; j < inEdges[i].size(); j++)
{
outNodes[inEdges[i][j]] -= ;
} toFind = true;
leftNode -= ;
break;
}
}
} return (leftNode == );
}
};

附加知识:pair的基本使用。

pair<int, int> p(1,2);

pair<int, int> p; p = make_pair(1,2);

p.first = p.second;

leetcode笔记(三)207. Course Schedule的更多相关文章

  1. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  2. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  3. LeetCode 笔记总结

    前言 之前把一些LeetCode题目的思路写在了本子上,现在把这些全都放到博客上,以后翻阅比较方便. 题目 99.Recover Binary Search Tree 题意 Two elements ...

  4. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  5. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  6. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  10. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

随机推荐

  1. linux shell之for循环

    两种方式 第一种 for((i=1;i<10;i++)) do echo $i done 第二种 for i in {1..10} do echo $i done

  2. JSP初学者2

    <jsp:useBean id="name" class="classname" scope="page|request|session|app ...

  3. hibernate 一览表

  4. 字符串安全处理:CRT安全增强以及安全模板重载

    Secure Template Overloads :http://msdn.microsoft.com/en-us/library/ms175759(v=vs.80).aspx Security E ...

  5. Oracle transport tablespace

    本来没想过发布这个文章,只是周边有一朋友工作中遇到合并数据库的情况,他是通过expdp提取出五个库对象,然后impdp到新库里面.我觉得这种方法特别耗时,尤其在数据量比较大的时候.这种时候我觉得采用表 ...

  6. 7 - py面向对象一条龙服务

    Python从设计之初就已经是一门面向对象的语言,在python里所有东西皆是对象. 下面通过一个实例来说明什么是面向对象. 引子 你是一家公司的员工,公司现在要开发一款“人狗战争”的游戏,人狗战争肯 ...

  7. 作为软件技术人员建立自己的git账户并保存资料的重要性

    日常生活中,当修改并保存了一个文件,所得到的就是此文件的最新版本,假若今后因某一问题需要用到原来文件,可是很多情况下,这种修改是不可逆的.你修改完之后,无法回到你修改前的样子.为了避免这种情况,有的人 ...

  8. 如何在SAP CRM里创建和消费Web service

    Created by Wang, Jerry, last modified on Dec 19, 2014 The following steps demonstrates how to expose ...

  9. 「C基础」位运算

    0. 原码.补码.反码 初学者只做了解即可 见 张子秋的博客 无论正负数,在内存中存储的都是补码 正数:反码 == 原码 == 补码 负数:反码 == ~原码 补码 == 反码+1 1. & ...

  10. hdu1215 七夕节---因子和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1215 题目大意: 求N的因子和(不包括N本身) 解题思路: 模板传送门 #include<io ...