leetcode笔记(三)207. Course Schedule
- 题目描述(原题目链接)
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:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- 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的更多相关文章
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- LeetCode 笔记总结
前言 之前把一些LeetCode题目的思路写在了本子上,现在把这些全都放到博客上,以后翻阅比较方便. 题目 99.Recover Binary Search Tree 题意 Two elements ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- 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 ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
随机推荐
- [LeetCode]23. Merge k Sorted Lists合并K个排序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- webpack缓存
缓存 缓存如何工作 1.当缓存客户端需要访问数据时,它首先检查缓存.当在缓存中找到所请求的数据时,它被称为缓存命中. 2.如果在缓存中找不到请求的数据 , 称为缓存未命中的情况,它将从主存储器中提取并 ...
- vue 上拉加载更多
var _this=this; var goods_id = _this.$route.query.id; var isscroll = true; _this.$nextTick(() => ...
- Instance Segmentation入门总结
前一阵子好忙啊,好久没更新了.最近正好挖了新坑,来更新下.因为之前是做检测的,而目前课题顺道偏到了instance segmentation,这篇文章简单梳理一下从检测.分割结果到instance s ...
- 嵌入式开发 MCU
From: http://www.infoq.com/cn/articles/intelligent-embedded-os-Internet-of-things-and-robots 嵌入式开发是一 ...
- JQuery Tips
另一篇文章 JavaScript Tips 1. 获取span标签的值需要用text(); 2. datepicker控件的‘setDate’属性可用于设置默认值: 3. 使用parseFloat转换 ...
- qt 创建线程
http://www.cnblogs.com/xinxue/p/6840315.html Qt 之 QtConcurrent 本文以 Qt 中的 QtConcurrent::run() 函数为例,介绍 ...
- matlab练习程序(全景图到穹顶图)
这个程序我最初是用FreeImage写的,这两天改成了matlab,再不贴上来,我就要忘了. 看到一篇文章有这样的变换,挺有意思的,就拿来试了一下,文章点此. 全景图到穹顶图变换,通俗的说就是将全景图 ...
- idea 新建文件夹目录问题解决
选中工程,设置,去掉选项“Compact Empty Middle Packages” .
- broadcastemit
http://code.angularjs.org/1.0.2/docs/api/ng.$rootScope.Scope#$broadcast scope可以以类似于DOM事件的方式进行事件传播.事件 ...