leetcode 210. Course Schedule II

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: 2, [[1,0]] 
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished   
             course 0. So the correct course order is [0,1] .

Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both     
             courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. 
             So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .

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.

解法:拓撲排序

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) 
    {
        vector<int> res ;
        unordered_set<int> cour_inde_zero ;
        unordered_map<int , int> cour_inde ;
        unordered_map<int , unordered_set<int>> cour_sufre ;
        
        for(int i = 0 ; i < numCourses ; ++i)
        {
            cour_inde_zero.insert(i) ;
        }
        
        for(auto pre : prerequisites)
        {
            cour_inde[pre[0]]++ ;
            cour_inde_zero.erase(pre[0]) ;
            cour_sufre[pre[1]].insert(pre[0]) ;
        }
        
        while(numCourses > 0)
        {
            if(cour_inde_zero.empty()) return {} ;
            int cour = *(cour_inde_zero.begin()) ;
            res.push_back(cour) ;
            cour_inde_zero.erase(cour) ;
            
            for(auto c : cour_sufre[cour])
            {
                cour_inde[c]-- ;
                if(cour_inde[c] == 0)
                {
                    cour_inde.erase(c) ;
                    cour_inde_zero.insert(c) ;
                }
            }
            
            numCourses-- ;
        }
        
        return res ;
    }
};

將哈希表換成向量,時間提升了十幾毫秒

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) 
    {
        vector<int> indegree(numCourses , 0) , res , cour_inde_zero;
        vector<vector<int>> cour_suf(numCourses) ;
        
        for(auto pre : prerequisites)
        {
            indegree[pre[0]]++ ;
            cour_suf[pre[1]].push_back(pre[0]) ;
        }
        
        for(int i = 0 ; i < numCourses ; ++i)
        {
            if(indegree[i] == 0) cour_inde_zero.push_back(i) ;
        }
        
        while(!cour_inde_zero.empty())
        {
            int cour = cour_inde_zero.back() ;
            cour_inde_zero.pop_back() ;
            res.push_back(cour) ;
            
            for(auto c : cour_suf[cour])
            {
                indegree[c]-- ;
                if(indegree[c] == 0) cour_inde_zero.push_back(c) ;
            }
        }
        
        return res.size() == numCourses ? res : vector<int>() ;
    }
};

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章