• 210. Course Schedule II


    https://leetcode.com/problems/course-schedule-ii/description/

    class Solution {
    public:
        vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
            vector<unordered_set<int>> v(numCourses);
            vector<int> indegrees(numCourses, 0);
            for (const auto& p : prerequisites) {
                v[p.second].insert(p.first);
                indegrees[p.first]++;
            }
            queue<int> q;
            for (int i = 0; i < numCourses; i++)
                if (indegrees[i] == 0)
                    q.push(i);
            vector<int> res;
            while (!q.empty()) {
                int cur = q.front();
                q.pop();
                res.push_back(cur);
                for (auto nb : v[cur]) {
                    indegrees[nb]--;
                    if (indegrees[nb] == 0)
                        q.push(nb);
                }
            }
            return res.size() == numCourses ? res : vector<int>();
        }
    };
    class Solution {
    public:
        vector<int> findOrder(int n, vector<pair<int, int>>& pres) {
            vector<int> res;    if (n < 1)  return res;
            
            vector<unordered_set<int>> v(n);
            vector<int> d(n, 0);                // in-degree of each node
            for (const auto& p : pres) {
                v[p.second].insert(p.first);
                d[p.first]++;
            }
            
            for (int k = 0; k < n; k++) {
                int cur = 0;   // find a v which indegree is 0, then we can delete it
                while (cur < n && d[cur] != 0) cur++;
                if (cur == n)   { return vector<int>(); }
                for (auto nb : v[cur])
                    d[nb]--;
                res.push_back(cur);
                d[cur] = -1;
            }
            return res;
        }
    };

     DFS:

    class Solution {
    public:
        vector<int> res;
        bool detectedCycle = false;
        vector<int> findOrder(int n, vector<pair<int, int>>& pres) {
            if (n < 1)  return res;
            
            vector<unordered_set<int>> v(n);
            for (const auto& p : pres)
                v[p.second].insert(p.first);
            
            vector<int> visited(n, -1);
            for (int i = 0; i < n; i++)
                dfs(v, visited, i);
            
            reverse(res.begin(), res.end());            // result in reverse order
            return detectedCycle ? vector<int>() : res;
        }
        void dfs(vector<unordered_set<int>>& v, vector<int>& visited, int n) {
            if (detectedCycle || visited[n] != -1)  return;
            visited[n] = 1;
            for (int i : v[n]) {
                if (visited[i] == 1)
                    detectedCycle = true;
                else if (visited[i] == 2)
                    continue;
                else
                    dfs(v, visited, i);
            }
            visited[n] = 2;
            res.push_back(n);       // push current node to the end, and final result is in reverse order.
        }
    };
  • 相关阅读:
    nohup 忽略 nohup.out 输出
    elementui 发送时间格式到 gin 后端问题
    在 Ubuntu 下使用 Puppeteer 时无法加载类库 libX11-xcb.so.1
    获取注解上的注解
    tomcat 远程 debug
    使用pytorch-lightning漂亮地进行深度学习研究(转)
    CTR预估模型发展历程(转)
    pytorch重要函数介绍
    Dataset和Dataloader
    推荐系统中的ID特征为什么有用?
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9000650.html
Copyright © 2020-2023  润新知