• LeetCode //C - 210. Course Schedule II


    210. Course Schedule II

    There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

    • For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.

    Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
     

    Example 1:

    Input: numCourses = 2, prerequisites = [[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: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
    Output: [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].

    Example 3:

    Input: numCourses = 1, prerequisites = []
    Output: [0]

    Constraints:
    • 1 <= numCourses <= 2000
    • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
    • prerequisites[i].length == 2
    • 0 <= a i , b i a_i, b_i ai,bi < numCourses
    • a i ! = b i a_i != b_i ai!=bi
    • All the pairs [ a i , b i a_i, b_i ai,bi] are distinct.

    From: LeetCode
    Link: 210. Course Schedule II


    Solution:

    Ideas:

    1. Represent the Courses as a Directed Graph:

    • Courses are represented as nodes in the graph.
    • The prerequisites are represented as directed edges in the graph, where an edge from course u to course v means v is a prerequisite for u.

    2. Calculate In-degree for each Course:

    • The in-degree of a node in a directed graph is the number of edges coming into it.
    • For every course, calculate its in-degree, i.e., the number of courses that have it as a prerequisite.

    3. Perform Topological Sort:

    • Initialize a queue and add all courses with in-degree 0 to it.
    • For each course u dequeued from the queue:
      • Add it to the result array.
      • For each course v adjacent to u, decrement the in-degree of v.
      • If the in-degree of v becomes 0, enqueue v.
    • If the size of the result array is equal to the number of courses, return the result array, else return an empty array.
    Code:
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* findOrder(int numCourses, int** prerequisites, int prerequisitesSize, int* prerequisitesColSize, int* returnSize) {
        int *result = (int *)malloc(numCourses * sizeof(int));
        if(!result) return NULL; // Return NULL if memory allocation fails
        
        int *inDegree = (int *)calloc(numCourses, sizeof(int));
        if(!inDegree) {
            free(result);
            return NULL;
        }
        
        int **adjList = (int **)malloc(numCourses * sizeof(int *));
        if(!adjList) {
            free(result);
            free(inDegree);
            return NULL;
        }
        
        for(int i = 0; i < numCourses; i++) {
            adjList[i] = (int *)malloc(numCourses * sizeof(int));
            if(!adjList[i]) {
                for(int j = 0; j < i; j++)
                    free(adjList[j]);
                free(adjList);
                free(inDegree);
                free(result);
                return NULL;
            }
            for(int j = 0; j < numCourses; j++)
                adjList[i][j] = 0;
        }
        
        for(int i = 0; i < prerequisitesSize; i++) {
            int course = prerequisites[i][0];
            int prereq = prerequisites[i][1];
            adjList[prereq][course] = 1; // prereq -> course
            inDegree[course]++;
        }
        
        int *queue = (int *)malloc(numCourses * sizeof(int));
        if(!queue) {
            for(int i = 0; i < numCourses; i++)
                free(adjList[i]);
            free(adjList);
            free(inDegree);
            free(result);
            return NULL;
        }
        
        int front = 0, rear = 0;
        
        for(int i = 0; i < numCourses; i++)
            if(inDegree[i] == 0)
                queue[rear++] = i;
        
        int count = 0;
        while(front < rear) {
            int v = queue[front++];
            result[count++] = v;
            for(int i = 0; i < numCourses; i++) {
                if(adjList[v][i]) {
                    inDegree[i]--;
                    if(inDegree[i] == 0)
                        queue[rear++] = i;
                }
            }
        }
        
        *returnSize = (count == numCourses) ? count : 0;
        
        for(int i = 0; i < numCourses; i++)
            free(adjList[i]);
        free(adjList);
        free(inDegree);
        free(queue);
        
        if(count < numCourses) {
            free(result);
            return NULL;
        }
        
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    [ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
    mindspore1.5版本下能正常运行到了1.6‘_check_version.py’报错
    取代 Mybatis Generator,这款代码生成神器配置更简单,开发效率更高!
    2023山东建筑大学考研介绍
    Zookeeper ACL机制中ProviderRegistry的设计缺陷
    【区分vue2和vue3下的element UI Card 卡片组件,分别详细介绍属性,事件,方法如何使用,并举例】
    计算机网络4小时速成:应用层,cs模型,p2p模型,DNS域名系统,文件传输协议FTP,电子邮件SMTP,万维网HTTP,动态主机配置协议DHCP
    E: Unable to locate package xxxx
    电子书(chm)-加载JS--CS上线
    视频特效-使用ffmpeg滤镜
  • 原文地址:https://blog.csdn.net/navicheung/article/details/133312965