zoukankan      html  css  js  c++  java
  • LeetCode "Course Schedule"

    Topological sorting. This is my DFS solution. Please note that I used _rec to avoid duplicate checking.

    class Solution {
    public:
        map<int, int> rec;
        set<int> _rec;
        bool go(map<int, vector<int>> &m, int inx, int level)
        {
            rec[inx] = level;
            _rec.insert(inx);
            for (auto i : m[inx])
            {
                if (rec[i] != -1 && rec[i] < level) return false;
    
                bool b = go(m, i, level + 1);
                if (!b) return false;
            }
            rec[inx] = -1;
            return true;
        }
        bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) 
        {
            map<int, vector<int>> m;
            for (auto &r : prerequisites)
            {
                m[r.second].push_back(r.first);
            }
            for (int i = 0; i < numCourses; i++)
                rec[i] = -1;
    
            for (auto &r : m)
            {
                if(_rec.find(r.first) != _rec.end()) continue;
                
                bool ret = go(m, r.first, 0);
                if (!ret) return false;
            }
            return true;
        }
    };
  • 相关阅读:
    hashlib模块
    sys模块
    random模块
    time模块
    生成器、迭代器
    装饰器
    函数相关内容
    集合相关操作
    springcloud(四):Eureka的配置详解
    springcloud(二):Eureka服务注册与发现
  • 原文地址:https://www.cnblogs.com/tonix/p/4500380.html
Copyright © 2011-2022 走看看