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. C#学习笔记7

    1.重写GetHashCode方法注意点: (1)重写GetHashCode方法,也应重写Equals方法,否者编译器会警告. (2)相等的对象必须有相等的散列码(若a.Equals(b),则a.Ge ...

  2. SQLAlchemy的使用---M2M增删改查

    from sqlalchemy.orm import sessionmaker from sqlalchemy_M2M import engine, Girls, Boys Session = ses ...

  3. 纯CSS实现Tab切换标签效果代码

    在线演示地址如下: http://demo.jb51.net/js/2015/css-tab-bq-style-cha-codes/ <!DOCTYPE html PUBLIC "-/ ...

  4. Angular选项卡

    前几天我发的东西,可能对于没有基础的人很难理解,那么今天,咱们就发点简单点的东西吧! Angular显示隐藏,选项卡! 还是那句话,话不多说,上代码: <!DOCTYPE html> &l ...

  5. C语言买卖股票问题

    遇到个简单的算法题,没有当场解出来,以后可以写伪代码表达思路. 数组中保存每天的股票价值,求买入卖出的时间和最大利润,比较好的解法如下: 伪代码: begin start day = 0; end d ...

  6. Mysql 服务无法启动解决办法

    1.我使用的是MySQL-5.7.10-winx64 版本,在安装后启动服务时出现 “服务无法启动”错误 2.解决办法为删除安装目录中的data文件,然后打开cmd调到自己的安装目录下输入mysqld ...

  7. OpenSUSE 内核编译教程 (kernel 2.6.x)

    http://cn.opensuse.org/OpenSUSE_%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91%E6%95%99%E7%A8%8B_(kernel_2.6.x ...

  8. java实现多文件上传01

    1.html代码 <html> <head> <link rel="stylesheet" type="text/css" hre ...

  9. OpenSSL.Net 在生产环境中无法正常加载的原因分析与解决 z

    http://blog.csdn.net/wangjia184/article/details/6990098 http://www.openssl.org/ 在本地测试好好的代码部署到生产环境后,遇 ...

  10. JSP-Servlet中文乱码

    客户端 get 方法时 出现乱码: 解决办法: String str1 = request.getParameter("stuname"); String str = new St ...