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 ...
随机推荐
- 线程安全的Dictionary
线程安全的DictionaryConcurrentDictionary<TKey, TValue> 类
- vue的拖拽文件
<div ref='select_frame' ondragstart="return false">//防止跳转 </div> this.$refs.se ...
- SENNA
SENNA is a software distributed under a non-commercial license, which outputs a host of Natural Lang ...
- 3D开源推荐:3DWebExplorer
开源网址:https://github.com/irconde/3DWebExplorer 介绍:演示如何内嵌Google Earth 插件,开发面向公众的3D旅游展示平台
- SpannableString与SpannableStringBuilder使用
转自:http://blog.it985.com/14433.html1.SpannableString.SpannableStringBuilder与String的关系 首先SpannableStr ...
- Oracle DG 三种模式(转)
DG有下面三种模式– Maximum protection– Maximum availability– Maximum performance 在Maximum protection下, 可以保证从 ...
- 【Spring实战】—— 4 Spring中bean的init和destroy方法讲解
本篇文章主要介绍了在spring中通过配置init-method和destroy-method方法来实现Bean的初始化和销毁时附加的操作. 在java中,我们并不需要去管理内存或者变量,而在C或C+ ...
- Smokeping外置邮箱告警
wget http://xrl.us/cpanm -O /usr/bin/cpanm 1.安装Authen::SASL模块 cpanm --mirror http://mirrors.163.com/ ...
- url规范化:解决从baidu.com到www.baidu.com的
通过 301重定向可以实现 把www.baidu.com和baidu.com合并,并把之前的域名也一并合并. 有两种实现方法: 第一种方法是判断nginx核心变量host(别名功能): server ...
- Facet constraits error: Spring 4.1 requires Java 1.6 or newer.
问题来源: 在高版本的myeclipse,同步低版本的myeclipse提交的项目,可能会出现配置不一致. 问题描述: spring4.1不支持jdk1.6 注:在下载项目到本地的时候,myeclip ...