算法W16——Course Schedule

题目:Course Schedule

链接:https://leetcode.com/problems/course-schedule/#/description

原题:

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?

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 it is possible.

2, [[1,0],[0,1]]

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.

解题思路:

本题使用暴力的深搜思路,先把限制条件用合适的容器存起来,然后将不受任何条件限制的课程进队列,用while循环遍历队列的每一门课程,并且将修过课程对别的课程的限制去掉,如果某一课程的限制被去掉变为零后,便可入队,如此循环得到答案。


代码:

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<bool> taked_course;
        vector<int> all_course;
        map<int, vector<int>> limit;
        for(int i=0; i<numCourses; i++) {
            taked_course.push_back(false);
            all_course.push_back(i);
        }
        queue<int> course_list;
        vector<pair<int, int>>::iterator it;
        for(it=prerequisites.begin(); it!=prerequisites.end(); it++) {
            if(limit.count((*it).second) != 0) {
                limit[(*it).second].push_back((*it).first);
            } else {
                vector<int> temp_vec;
                temp_vec.push_back((*it).first);
                limit[(*it).second] = temp_vec;
            }
            
            vector<int>::iterator find_loc = find(all_course.begin(), all_course.end(), (*it).second);
            if(find_loc != all_course.end()) {
                all_course.erase(find_loc);
            }
        }
        
        if(all_course.empty()) {
            return false;
        }
        for(vector<int>::iterator itt=all_course.begin(); itt!=all_course.end(); itt++) {
            course_list.push(*itt);
        }
        
        while(!course_list.empty()) {
            int course_id = course_list.front();
            taked_course[course_id] = true;
            for(it=prerequisites.begin(); it!=prerequisites.end(); it++) {
                if((*it).first == course_id) {
                    vector<int>::iterator find_loc = find(limit[(*it).second].begin(), limit[(*it).second].end(), (*it).first);
                    limit[(*it).second].erase(find_loc);
                    if(limit[(*it).second].empty()) {
                        course_list.push((*it).second);
                        prerequisites.erase(it);
                        it--;
                    }
                }
            }
            course_list.pop();
        }
        if(find(taked_course.begin(), taked_course.end(), false) != taked_course.end()) {
            return false;
        } else {
            return true;
        }
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值